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

better error for bad setState arguments and mergeHelpers #822

Merged
merged 2 commits into from
Jan 17, 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
12 changes: 12 additions & 0 deletions src/core/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,18 @@ var ReactCompositeComponentMixin = {
* @protected
*/
setState: function(partialState, callback) {
invariant(
typeof partialState === 'object' || partialState == null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... why did you allow null / undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for empty setState() after you mutate the state. Not the best practice ever though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I don't see a way to trigger a possible update without doing this (forceUpdate won't call shouldUpdateComponent), but it would be better if there was, as passing null into setState can often be an error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're mutating the data, shouldComponentUpdate is helpless. I think we should disallow null and tell people to use forceUpdate() if they ask. (Or setState({}) if for some reason shouldComponentUpdate needs to be called.)

'setState(...): takes an object of state variables to update.'
);
if (__DEV__){
if (partialState == null) {
console.warn(
'setState(...): You passed an undefined or null state object; ' +
'instead, use forceUpdate().'
);
}
}
// Merge with `_pendingState` if it exists, otherwise with existing state.
this.replaceState(
merge(this._pendingState || this.state, partialState),
Expand Down
11 changes: 5 additions & 6 deletions src/vendor/core/mergeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ var mergeHelpers = {
checkMergeArrayArgs: function(one, two) {
invariant(
Array.isArray(one) && Array.isArray(two),
'Critical assumptions about the merge functions have been violated. ' +
'This is the fault of the merge functions themselves, not necessarily ' +
'the callers.'
'Tried to merge arrays, instead got %s and %s.',
one,
two
);
},

Expand All @@ -87,9 +87,8 @@ var mergeHelpers = {
checkMergeObjectArg: function(arg) {
invariant(
!isTerminal(arg) && !Array.isArray(arg),
'Critical assumptions about the merge functions have been violated. ' +
'This is the fault of the merge functions themselves, not necessarily ' +
'the callers.'
'Tried to merge an object, instead got %s.',
arg
);
},

Expand Down