Warn if findDOMNode is used inside render
Need to reset current owner to null before committing
Andrew Clark committed
Feb 7, 2017 at 12:13 UTC
76a168fed6b8a2823c668ac13ca868434e16c4e9
4 files changed
+16
-7
scripts/fiber/tests-passing-except-dev.txt
-3
@@ -1,6 +1,3 @@
1
-src/renderers/__tests__/ReactComponentLifeCycle-test.js
2
-* warns if findDOMNode is used inside render
3
-
1
src/renderers/__tests__/ReactComponentTreeHook-test.js
2
* uses displayName or Unknown for classic components
3
* uses displayName, name, or ReactComponent for modern components
scripts/fiber/tests-passing.txt
+1
@@ -550,6 +550,7 @@ src/renderers/__tests__/ReactComponentLifeCycle-test.js
550
* should correctly determine if a component is mounted
551
* should correctly determine if a null component is mounted
552
* isMounted should return false when unmounted
553
+* warns if findDOMNode is used inside render
554
* should not throw when updating an auxiliary component
555
* should allow state updates in componentDidMount
556
* should call nested lifecycle methods in the right order
src/renderers/dom/shared/findDOMNode.js
+12
-4
@@ -26,10 +26,14 @@ let findStack = function(arg) {
26
27
const findDOMNode = function(componentOrElement : Element | ?ReactComponent<any, any, any>) : null | Element | Text {
28
if (__DEV__) {
29
- var owner = ReactCurrentOwner.current;
30
- if (owner !== null && '_warnedAboutRefsInRender' in owner) {
29
+ var owner = (ReactCurrentOwner.current : any);
30
+ if (owner !== null) {
31
+ var isFiber = typeof owner.tag === 'number';
32
+ var warnedAboutRefsInRender = isFiber ?
33
+ owner.stateNode._warnedAboutRefsInRender :
34
+ owner._warnedAboutRefsInRender;
35
warning(
32
- (owner: any)._warnedAboutRefsInRender,
36
+ warnedAboutRefsInRender,
37
'%s is accessing findDOMNode inside its render(). ' +
38
'render() should be a pure function of props and state. It should ' +
39
'never access something that requires stale data from the previous ' +
@@ -37,7 +41,11 @@ const findDOMNode = function(componentOrElement : Element | ?ReactComponent<any,
41
'componentDidUpdate instead.',
42
getComponentName(owner) || 'A component'
43
);
40
- (owner: any)._warnedAboutRefsInRender = true;
44
+ if (isFiber) {
45
+ owner.stateNode._warnedAboutRefsInRender = true;
46
+ } else {
47
+ owner._warnedAboutRefsInRender = true;
48
+ }
49
}
50
}
51
if (componentOrElement == null) {
src/renderers/shared/fiber/ReactFiberScheduler.js
+3
@@ -349,6 +349,9 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
349
'in React. Please file an issue.'
350
);
351
352
+ // Reset this to null before calling lifecycles
353
+ ReactCurrentOwner.current = null;
354
+
355
// Updates that occur during the commit phase should have Task priority
356
const previousPriorityContext = priorityContext;
357
priorityContext = TaskPriority;