findNodeHandle properly handles ReactCurrentOwner.current Fiber type

Reviewed By: spicyj

Differential Revision: D5005265

fbshipit-source-id: 218ba3461514fa1dd603ad53c129dd37d7309050
This commit is contained in:
Brian Vaughn 2017-05-04 16:02:07 -07:00 committed by Facebook Github Bot
parent 857be044cc
commit 909fb7239d
1 changed files with 16 additions and 5 deletions

View File

@ -15,9 +15,11 @@
var ReactInstanceMap = require('ReactInstanceMap'); var ReactInstanceMap = require('ReactInstanceMap');
var {ReactCurrentOwner} = require('ReactGlobalSharedState'); var {ReactCurrentOwner} = require('ReactGlobalSharedState');
var getComponentName = require('getComponentName');
var invariant = require('fbjs/lib/invariant'); var invariant = require('fbjs/lib/invariant');
var warning = require('fbjs/lib/warning'); var warning = require('fbjs/lib/warning');
import type {Fiber} from 'ReactFiber';
import type {ReactInstance} from 'ReactInstanceType'; import type {ReactInstance} from 'ReactInstanceType';
/** /**
@ -58,20 +60,29 @@ let injectedFindRootNodeID;
// accidentally deep-requiring this version. // accidentally deep-requiring this version.
function findNodeHandle(componentOrHandle: any): any { function findNodeHandle(componentOrHandle: any): any {
if (__DEV__) { if (__DEV__) {
// TODO: fix this unsafe cast to work with Fiber. var owner =
var owner = ((ReactCurrentOwner.current: any): ReactInstance | null); ((ReactCurrentOwner.current: any): ReactInstance | Fiber | null);
if (owner !== null) { if (owner !== null) {
const isFiber = typeof (owner: any).tag === 'number';
const warnedAboutRefsInRender = isFiber
? ((owner: any): Fiber).stateNode._warnedAboutRefsInRender
: ((owner: any): ReactInstance)._warnedAboutRefsInRender;
warning( warning(
owner._warnedAboutRefsInRender, warnedAboutRefsInRender,
'%s is accessing findNodeHandle inside its render(). ' + '%s is accessing findNodeHandle inside its render(). ' +
'render() should be a pure function of props and state. It should ' + 'render() should be a pure function of props and state. It should ' +
'never access something that requires stale data from the previous ' + 'never access something that requires stale data from the previous ' +
'render, such as refs. Move this logic to componentDidMount and ' + 'render, such as refs. Move this logic to componentDidMount and ' +
'componentDidUpdate instead.', 'componentDidUpdate instead.',
owner.getName() || 'A component', getComponentName(owner) || 'A component',
); );
owner._warnedAboutRefsInRender = true; if (isFiber) {
((owner: any): Fiber).stateNode._warnedAboutRefsInRender = true;
} else {
((owner: any): ReactInstance)._warnedAboutRefsInRender = true;
}
} }
} }
if (componentOrHandle == null) { if (componentOrHandle == null) {