Skip to content

Commit

Permalink
Warn instead of throw for nested render calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheng Lou authored and zpao committed Apr 27, 2014
1 parent c9767c2 commit 8b23a7e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/browser/ui/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var getReactRootElementInContainer = require('getReactRootElementInContainer');
var instantiateReactComponent = require('instantiateReactComponent');
var invariant = require('invariant');
var shouldUpdateReactComponent = require('shouldUpdateReactComponent');
var warning = require('warning');

var SEPARATOR = ReactInstanceHandles.SEPARATOR;

Expand Down Expand Up @@ -301,7 +302,7 @@ var ReactMount = {
// Various parts of our code (such as ReactCompositeComponent's
// _renderValidatedComponent) assume that calls to render aren't nested;
// verify that that's the case.
invariant(
warning(
ReactCurrentOwner.current == null,
'_renderNewRootComponent(): Render methods should be a pure function ' +
'of props and state; triggering nested component updates from ' +
Expand Down Expand Up @@ -440,7 +441,7 @@ var ReactMount = {
// _renderValidatedComponent) assume that calls to render aren't nested;
// verify that that's the case. (Strictly speaking, unmounting won't cause a
// render but we still don't expect to be in a render call here.)
invariant(
warning(
ReactCurrentOwner.current == null,
'unmountComponentAtNode(): Render methods should be a pure function of ' +
'props and state; triggering nested component updates from render is ' +
Expand Down
3 changes: 2 additions & 1 deletion src/core/ReactUpdates.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactPerf = require('ReactPerf');

var invariant = require('invariant');
var warning = require('warning');

var dirtyComponents = [];

Expand Down Expand Up @@ -109,7 +110,7 @@ function enqueueUpdate(component, callback) {
// verify that that's the case. (This is called by each top-level update
// function, like setProps, setState, forceUpdate, etc.; creation and
// destruction of top-level components is guarded in ReactMount.)
invariant(
warning(
ReactCurrentOwner.current == null,
'enqueueUpdate(): Render methods should be a pure function of props ' +
'and state; triggering nested component updates from render is not ' +
Expand Down
9 changes: 5 additions & 4 deletions src/core/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,7 @@ describe('ReactCompositeComponent', function() {
});

it('should disallow nested render calls', function() {
spyOn(console, 'warn');
var Inner = React.createClass({
render: function() {
return <div />;
Expand All @@ -1358,10 +1359,10 @@ describe('ReactCompositeComponent', function() {
}
});

expect(() => {
ReactTestUtils.renderIntoDocument(<Outer />);
}).toThrow(
'Invariant Violation: _renderNewRootComponent(): Render methods should ' +
ReactTestUtils.renderIntoDocument(<Outer />);
expect(console.warn.argsForCall.length).toBe(1);
expect(console.warn.argsForCall[0][0]).toBe(
'Warning: _renderNewRootComponent(): Render methods should ' +
'be a pure function of props and state; triggering nested component ' +
'updates from render is not allowed. If necessary, trigger nested ' +
'updates in componentDidUpdate.'
Expand Down

0 comments on commit 8b23a7e

Please sign in to comment.