Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error for nested render calls #1402

Merged
merged 2 commits into from
Apr 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/browser/ui/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"use strict";

var DOMProperty = require('DOMProperty');
var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactEventEmitter = require('ReactEventEmitter');
var ReactInstanceHandles = require('ReactInstanceHandles');
var ReactPerf = require('ReactPerf');
Expand Down Expand Up @@ -297,6 +298,16 @@ var ReactMount = {
nextComponent,
container,
shouldReuseMarkup) {
// 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(
ReactCurrentOwner.current == null,
'_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.'
);

var componentInstance = instantiateReactComponent(nextComponent);
var reactRootID = ReactMount._registerComponent(
Expand Down Expand Up @@ -425,6 +436,18 @@ var ReactMount = {
* `container`
*/
unmountComponentAtNode: function(container) {
// Various parts of our code (such as ReactCompositeComponent's
// _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(
ReactCurrentOwner.current == null,
'unmountComponentAtNode(): 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.'
);

var reactRootID = getReactRootID(container);
var component = instancesByReactRootID[reactRootID];
if (!component) {
Expand Down
14 changes: 14 additions & 0 deletions src/core/ReactUpdates.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

"use strict";

var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactPerf = require('ReactPerf');

var invariant = require('invariant');
Expand Down Expand Up @@ -103,6 +104,19 @@ function enqueueUpdate(component, callback) {
);
ensureBatchingStrategy();

// Various parts of our code (such as ReactCompositeComponent's
// _renderValidatedComponent) assume that calls to render aren't nested;
// 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(
ReactCurrentOwner.current == null,
'enqueueUpdate(): 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.'
);

if (!batchingStrategy.isBatchingUpdates) {
component.performUpdateIfNecessary();
callback && callback.call(component);
Expand Down
23 changes: 23 additions & 0 deletions src/core/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1345,4 +1345,27 @@ describe('ReactCompositeComponent', function() {
expect(console.warn.argsForCall.length).toBe(0);
});

it('should disallow nested render calls', function() {
var Inner = React.createClass({
render: function() {
return <div />;
}
});
var Outer = React.createClass({
render: function() {
ReactTestUtils.renderIntoDocument(<Inner />);
return <div />;
}
});

expect(() => {
ReactTestUtils.renderIntoDocument(<Outer />);
}).toThrow(
'Invariant Violation: _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.'
);
});

});