mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Warn instead of throw for nested render calls
This commit is contained in:
committed by
Paul O’Shannessy
parent
c9767c2822
commit
8b23a7e699
@@ -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 ' +
|
||||
|
||||
@@ -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.'
|
||||
|
||||
Reference in New Issue
Block a user