Skip to content

Commit

Permalink
Fix Issue facebook#11593: Warn if this.state is set to this.props ref…
Browse files Browse the repository at this point in the history
…erentially
  • Loading branch information
Veekas Shrivastava committed Nov 25, 2017
1 parent 1952d3b commit d053efb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,8 @@ describe('ReactComponentLifeCycle', () => {
'this.state should not be set to this.props referentially. When ' +
'implementing the constructor for a React.Component subclass, you ' +
'should call super(props) before any other statement. To initialize ' +
'state locally, just assign an object to this.state in the constructor.'
'state locally, just assign an object to this.state in the constructor.',
);

});

it('should not allow update state inside of getInitialState', () => {
Expand Down
19 changes: 9 additions & 10 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,6 @@ export default function(
);
}
}
const noSetStatesFromProps = !(instance.state === parent.props);
warning(
noSetStatesFromProps,
// console.log('instance.state', instance.state) +
// console.log('workInProgress', type) +
'this.state should not be set to this.props referentially. When ' +
'implementing the constructor for a React.Component subclass, you ' +
'should call super(props) before any other statement. To initialize ' +
'state locally, just assign an object to this.state in the constructor.'
);
const noGetInitialStateOnES6 =
!instance.getInitialState ||
instance.getInitialState.isReactClassApproved ||
Expand Down Expand Up @@ -474,6 +464,15 @@ export default function(
instance.refs = emptyObject;
instance.context = getMaskedContext(workInProgress, unmaskedContext);

const noSetStatesFromProps = !(instance.state === instance.props);
warning(
noSetStatesFromProps,
'this.state should not be set to this.props referentially. When ' +
'implementing the constructor for a React.Component subclass, you ' +
'should call super(props) before any other statement. To initialize ' +
'state locally, just assign an object to this.state in the constructor.',
);

if (
enableAsyncSubtreeAPI &&
workInProgress.type != null &&
Expand Down

0 comments on commit d053efb

Please sign in to comment.