mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Warn about setState in render
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user