Warn if findDOMNode is used inside render

Need to reset current owner to null before committing
This commit is contained in:
Andrew Clark
2017-02-08 10:32:14 -08:00
parent 4d52ebc066
commit 76a168fed6
4 changed files with 16 additions and 7 deletions
@@ -1,6 +1,3 @@
src/renderers/__tests__/ReactComponentLifeCycle-test.js
* warns if findDOMNode is used inside render
src/renderers/__tests__/ReactComponentTreeHook-test.js
* uses displayName or Unknown for classic components
* uses displayName, name, or ReactComponent for modern components
+1
View File
@@ -550,6 +550,7 @@ src/renderers/__tests__/ReactComponentLifeCycle-test.js
* should correctly determine if a component is mounted
* should correctly determine if a null component is mounted
* isMounted should return false when unmounted
* warns if findDOMNode is used inside render
* should not throw when updating an auxiliary component
* should allow state updates in componentDidMount
* should call nested lifecycle methods in the right order
+12 -4
View File
@@ -26,10 +26,14 @@ let findStack = function(arg) {
const findDOMNode = function(componentOrElement : Element | ?ReactComponent<any, any, any>) : null | Element | Text {
if (__DEV__) {
var owner = ReactCurrentOwner.current;
if (owner !== null && '_warnedAboutRefsInRender' in owner) {
var owner = (ReactCurrentOwner.current : any);
if (owner !== null) {
var isFiber = typeof owner.tag === 'number';
var warnedAboutRefsInRender = isFiber ?
owner.stateNode._warnedAboutRefsInRender :
owner._warnedAboutRefsInRender;
warning(
(owner: any)._warnedAboutRefsInRender,
warnedAboutRefsInRender,
'%s is accessing findDOMNode inside its render(). ' +
'render() should be a pure function of props and state. It should ' +
'never access something that requires stale data from the previous ' +
@@ -37,7 +41,11 @@ const findDOMNode = function(componentOrElement : Element | ?ReactComponent<any,
'componentDidUpdate instead.',
getComponentName(owner) || 'A component'
);
(owner: any)._warnedAboutRefsInRender = true;
if (isFiber) {
owner.stateNode._warnedAboutRefsInRender = true;
} else {
owner._warnedAboutRefsInRender = true;
}
}
}
if (componentOrElement == null) {
@@ -349,6 +349,9 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
'in React. Please file an issue.'
);
// Reset this to null before calling lifecycles
ReactCurrentOwner.current = null;
// Updates that occur during the commit phase should have Task priority
const previousPriorityContext = priorityContext;
priorityContext = TaskPriority;