Warn instead of throw for nested render calls
Cheng Lou committed
Apr 27, 2014 at 13:15 UTC
8b23a7e699972bf837decf3ea402ecc1b864f76e
3 files changed
+10
-7
src/browser/ui/ReactMount.js
+3
-2
@@ -29,6 +29,7 @@ var getReactRootElementInContainer = require('getReactRootElementInContainer');
29
var instantiateReactComponent = require('instantiateReactComponent');
30
var invariant = require('invariant');
31
var shouldUpdateReactComponent = require('shouldUpdateReactComponent');
32
+var warning = require('warning');
33
34
var SEPARATOR = ReactInstanceHandles.SEPARATOR;
35
@@ -301,7 +302,7 @@ var ReactMount = {
302
// Various parts of our code (such as ReactCompositeComponent's
303
// _renderValidatedComponent) assume that calls to render aren't nested;
304
// verify that that's the case.
304
- invariant(
305
+ warning(
306
ReactCurrentOwner.current == null,
307
'_renderNewRootComponent(): Render methods should be a pure function ' +
308
'of props and state; triggering nested component updates from ' +
@@ -440,7 +441,7 @@ var ReactMount = {
441
// _renderValidatedComponent) assume that calls to render aren't nested;
442
// verify that that's the case. (Strictly speaking, unmounting won't cause a
443
// render but we still don't expect to be in a render call here.)
443
- invariant(
444
+ warning(
445
ReactCurrentOwner.current == null,
446
'unmountComponentAtNode(): Render methods should be a pure function of ' +
447
'props and state; triggering nested component updates from render is ' +
src/core/ReactUpdates.js
+2
-1
@@ -22,6 +22,7 @@ var ReactCurrentOwner = require('ReactCurrentOwner');
22
var ReactPerf = require('ReactPerf');
23
24
var invariant = require('invariant');
25
+var warning = require('warning');
26
27
var dirtyComponents = [];
28
@@ -109,7 +110,7 @@ function enqueueUpdate(component, callback) {
110
// verify that that's the case. (This is called by each top-level update
111
// function, like setProps, setState, forceUpdate, etc.; creation and
112
// destruction of top-level components is guarded in ReactMount.)
112
- invariant(
113
+ warning(
114
ReactCurrentOwner.current == null,
115
'enqueueUpdate(): Render methods should be a pure function of props ' +
116
'and state; triggering nested component updates from render is not ' +
src/core/__tests__/ReactCompositeComponent-test.js
+5
-4
@@ -1346,6 +1346,7 @@ describe('ReactCompositeComponent', function() {
1346
});
1347
1348
it('should disallow nested render calls', function() {
1349
+ spyOn(console, 'warn');
1350
var Inner = React.createClass({
1351
render: function() {
1352
return <div />;
@@ -1358,10 +1359,10 @@ describe('ReactCompositeComponent', function() {
1359
}
1360
});
1361
1361
- expect(() => {
1362
- ReactTestUtils.renderIntoDocument(<Outer />);
1363
- }).toThrow(
1364
- 'Invariant Violation: _renderNewRootComponent(): Render methods should ' +
1362
+ ReactTestUtils.renderIntoDocument(<Outer />);
1363
+ expect(console.warn.argsForCall.length).toBe(1);
1364
+ expect(console.warn.argsForCall[0][0]).toBe(
1365
+ 'Warning: _renderNewRootComponent(): Render methods should ' +
1366
'be a pure function of props and state; triggering nested component ' +
1367
'updates from render is not allowed. If necessary, trigger nested ' +
1368
'updates in componentDidUpdate.'