Warn about setState in render

This commit is contained in:
Andrew Clark
2017-02-24 11:02:21 -08:00
parent 769c6e0ebe
commit 2f3df5f0b3
3 changed files with 18 additions and 1 deletions
@@ -106,7 +106,6 @@ src/renderers/__tests__/ReactComponentTreeHook-test.native.js
* does not report top-level wrapper as a root
src/renderers/__tests__/ReactCompositeComponent-test.js
* should warn about `setState` in render
* should warn about `setState` in getChildContext
* should disallow nested render calls
+1
View File
@@ -577,6 +577,7 @@ src/renderers/__tests__/ReactCompositeComponent-test.js
* should warn about `forceUpdate` on unmounted components
* should warn about `setState` on unmounted components
* should silently allow `setState`, not call cb on unmounting components
* should warn about `setState` in render
* should cleanup even if render() fatals
* should call componentWillUnmount before unmounting
* should warn when shouldComponentUpdate() returns undefined
@@ -102,6 +102,16 @@ if (__DEV__) {
ctor && (ctor.displayName || ctor.name) || 'ReactClass'
);
};
var warnAboutUpdateInRender = function(instance : ReactClass<any>) {
warning(
ReactCurrentOwner.current == null,
'Cannot update during an existing state transition (such as within ' +
'`render` or another component\'s constructor). Render methods should ' +
'be a pure function of props and state; constructor side-effects are ' +
'an anti-pattern, but can be moved to `componentWillMount`.'
);
};
}
var timeHeuristicForUnitOfWork = 1;
@@ -1102,6 +1112,13 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
nextUnitOfWork = null;
}
if (__DEV__) {
if (fiber.tag === ClassComponent) {
const instance = fiber.stateNode;
warnAboutUpdateInRender(instance);
}
}
let node = fiber;
let shouldContinue = true;
while (node !== null && shouldContinue) {