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

Warn when setState called within getChildContext #6135

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/renderers/shared/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ var ReactCompositeComponentMixin = {
this._pendingStateQueue = null;
this._pendingReplaceState = false;
this._pendingForceUpdate = false;
this._processingChildContext = false;

this._renderedNodeType = null;
this._renderedComponent = null;
Expand Down Expand Up @@ -496,7 +497,9 @@ var ReactCompositeComponentMixin = {
_processChildContext: function(currentContext) {
var Component = this._currentElement.type;
var inst = this._instance;
this._processingChildContext = true;
var childContext = inst.getChildContext && inst.getChildContext();
this._processingChildContext = false;
if (childContext) {
invariant(
typeof Component.childContextTypes === 'object',
Expand Down
6 changes: 6 additions & 0 deletions src/renderers/shared/reconciler/ReactUpdateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ function getInternalInstanceReadyForUpdate(publicInstance, callerName) {
}

if (__DEV__) {
warning(
!internalInstance._processingChildContext,
'%s(...): Cannot update during processing child context.',
callerName
);

warning(
ReactCurrentOwner.current == null,
'%s(...): Cannot update during an existing state transition (such as ' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,32 @@ describe('ReactCompositeComponent', function() {
expect(innerUnmounted).toBe(true);
});

it('should warn when setState() called within getChildContext()', function() {
var Component = React.createClass({
childContextTypes: {
bogus: ReactPropTypes.bool.isRequired,
},

getChildContext: function() {
this.setState({a: 1});
return {bogus: false};
},

render: function() {
return <div />;
},
});

expect(() => {
ReactTestUtils.renderIntoDocument(<Component />);
}).toThrow('Maximum call stack size exceeded');

expect(console.error.argsForCall.length).toBeGreaterThan(0);
expect(console.error.argsForCall[0][0]).toBe(
'Warning: setState(...): Cannot update during processing child context.'
);
});

it('should warn when shouldComponentUpdate() returns undefined', function() {
var Component = React.createClass({
getInitialState: function() {
Expand Down