-
Notifications
You must be signed in to change notification settings - Fork 47k
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 not-yet-mounted setState warning #12531
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,15 @@ function warnNoop(publicInstance, callerName) { | |
} | ||
warning( | ||
false, | ||
'%s(...): Can only update a mounted or mounting component. ' + | ||
'This usually means you called %s() on an unmounted component. ' + | ||
'This is a no-op.\n\nPlease check the code for the %s component.', | ||
callerName, | ||
"Can't call %s on a component that is not yet mounted. " + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overall, I like this message better- but this first sentence seems less accurate and maybe confusing, since you can call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we don't want to encourage those methods (and will start warning for them in the future) I wouldn't worry about them. |
||
'This is a no-op, but it might indicate a bug in your application.\n\n' + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't call %s on a component before mounting it. This is a no-op, but it likely indicates a bug in your application. Instead, assign to `this.state` directly or define a `state = {};` class property with the desired state. ^ Maybe? It looks a little wordy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean you want to drop the second part? I thought it's kind of important because these issues will get more prominent with async. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I see what you mean. If this only affects constructor we might as well not talk about this. |
||
'To fix, assign the initial state in the %s constructor. ' + | ||
'If the state needs to reflect an external data source, ' + | ||
'you may also add a componentDidMount lifecycle hook to %s ' + | ||
'and call setState there if the external data has changed.', | ||
callerName, | ||
componentName, | ||
componentName, | ||
); | ||
didWarnStateUpdateForUnmountedComponent[warningKey] = true; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note these are not new warnings. Just new tests for an existing warning.