Warn instead of throw for nested render calls

This commit is contained in:
Cheng Lou
2014-04-27 13:15:42 -07:00
committed by Paul O’Shannessy
parent c9767c2822
commit 8b23a7e699
3 changed files with 10 additions and 7 deletions
+3 -2
View File
@@ -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;
@@ -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 ' +
@@ -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 ' +
+2 -1
View File
@@ -22,6 +22,7 @@ var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactPerf = require('ReactPerf');
var invariant = require('invariant');
var warning = require('warning');
var dirtyComponents = [];
@@ -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 ' +
@@ -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 />;
@@ -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.'