Warn about setState in render
Andrew Clark committed
Feb 7, 2017 at 13:49 UTC
2f3df5f0b3dea452cd64508f760ff8684f7ce971
3 files changed
+18
-1
scripts/fiber/tests-passing-except-dev.txt
-1
@@ -106,7 +106,6 @@ src/renderers/__tests__/ReactComponentTreeHook-test.native.js
106
* does not report top-level wrapper as a root
107
108
src/renderers/__tests__/ReactCompositeComponent-test.js
109
-* should warn about `setState` in render
109
* should warn about `setState` in getChildContext
110
* should disallow nested render calls
111
scripts/fiber/tests-passing.txt
+1
@@ -577,6 +577,7 @@ src/renderers/__tests__/ReactCompositeComponent-test.js
577
* should warn about `forceUpdate` on unmounted components
578
* should warn about `setState` on unmounted components
579
* should silently allow `setState`, not call cb on unmounting components
580
+* should warn about `setState` in render
581
* should cleanup even if render() fatals
582
* should call componentWillUnmount before unmounting
583
* should warn when shouldComponentUpdate() returns undefined
src/renderers/shared/fiber/ReactFiberScheduler.js
+17
@@ -102,6 +102,16 @@ if (__DEV__) {
102
ctor && (ctor.displayName || ctor.name) || 'ReactClass'
103
);
104
};
105
+
106
+ var warnAboutUpdateInRender = function(instance : ReactClass<any>) {
107
+ warning(
108
+ ReactCurrentOwner.current == null,
109
+ 'Cannot update during an existing state transition (such as within ' +
110
+ '`render` or another component\'s constructor). Render methods should ' +
111
+ 'be a pure function of props and state; constructor side-effects are ' +
112
+ 'an anti-pattern, but can be moved to `componentWillMount`.'
113
+ );
114
+ };
115
}
116
117
var timeHeuristicForUnitOfWork = 1;
@@ -1102,6 +1112,13 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
1112
nextUnitOfWork = null;
1113
}
1114
1115
+ if (__DEV__) {
1116
+ if (fiber.tag === ClassComponent) {
1117
+ const instance = fiber.stateNode;
1118
+ warnAboutUpdateInRender(instance);
1119
+ }
1120
+ }
1121
+
1122
let node = fiber;
1123
let shouldContinue = true;
1124
while (node !== null && shouldContinue) {