Skip to content

Commit d8cdefe

Browse files
committed
Warn about nested renders
1 parent 58f087c commit d8cdefe

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

scripts/fiber/tests-passing-except-dev.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ src/renderers/__tests__/ReactComponentTreeHook-test.js
5656
* registers inlined text nodes
5757
* works
5858

59-
src/renderers/__tests__/ReactCompositeComponent-test.js
60-
* should disallow nested render calls
61-
6259
src/renderers/__tests__/ReactHostOperationHistoryHook-test.js
6360
* gets recorded for host roots
6461
* gets recorded for composite roots

scripts/fiber/tests-passing.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ src/renderers/__tests__/ReactCompositeComponent-test.js
639639
* should pass context when re-rendered
640640
* unmasked context propagates through updates
641641
* should trigger componentWillReceiveProps for context changes
642+
* should disallow nested render calls
642643
* only renders once if updated in componentWillReceiveProps
643644
* only renders once if updated in componentWillReceiveProps when batching
644645
* should update refs if shouldComponentUpdate gives false

src/renderers/__tests__/ReactCompositeComponent-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,11 +1028,11 @@ describe('ReactCompositeComponent', () => {
10281028

10291029
ReactTestUtils.renderIntoDocument(<Outer />);
10301030
expectDev(console.error.calls.count()).toBe(1);
1031-
expectDev(console.error.calls.argsFor(0)[0]).toBe(
1032-
'Warning: _renderNewRootComponent(): Render methods should ' +
1033-
'be a pure function of props and state; triggering nested component ' +
1034-
'updates from render is not allowed. If necessary, trigger nested ' +
1035-
'updates in componentDidUpdate.\n\nCheck the render method of Outer.'
1031+
expectDev(console.error.calls.argsFor(0)[0]).toMatch(
1032+
'Render methods should be a pure function of props and state; ' +
1033+
'triggering nested component updates from render is not allowed. If ' +
1034+
'necessary, trigger nested updates in componentDidUpdate.\n\nCheck the ' +
1035+
'render method of Outer.'
10361036
);
10371037
});
10381038

src/renderers/shared/fiber/ReactFiberReconciler.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ var ReactFiberScheduler = require('ReactFiberScheduler');
3131

3232
if (__DEV__) {
3333
var ReactFiberInstrumentation = require('ReactFiberInstrumentation');
34+
var warning = require('warning');
35+
var ReactCurrentOwner = require('ReactCurrentOwner');
36+
var { getComponentName } = require('ReactFiberTreeReflection');
3437
}
3538

3639
var { findCurrentHostFiber } = require('ReactFiberTreeReflection');
@@ -143,6 +146,21 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
143146
} = ReactFiberScheduler(config);
144147

145148
function scheduleTopLevelUpdate(current : Fiber, element : ReactNodeList, callback : ?Function) {
149+
if (__DEV__) {
150+
const owner = ReactCurrentOwner.current;
151+
if (owner && typeof owner.tag === 'number') {
152+
const ownerFiber : Fiber = (owner : any);
153+
warning(
154+
false,
155+
'Render methods should be a pure function of props and state; ' +
156+
'triggering nested component updates from render is not allowed. ' +
157+
'If necessary, trigger nested updates in componentDidUpdate.\n\n' +
158+
'Check the render method of %s.',
159+
getComponentName(ownerFiber)
160+
);
161+
}
162+
}
163+
146164
const priorityLevel = getPriorityContext();
147165
const nextState = { element };
148166
addTopLevelUpdate(current, nextState, callback || null, priorityLevel);

0 commit comments

Comments
 (0)