-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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 if this.state is set to this.props referentially #11593
Comments
Happy to take! edit: instead of doing this myself i am working with @veekas to get him started contributing to react, below |
Sounds good. |
I'm a new contributor, but working with @sw-yx on this. I'm currently trying to figure out how to catch if the user is trying to set I currently am looking at putting the warning in // the following is in the checkClassInstance function
const noSetStatesFromProps = !(instance.state === parent.props); // this logic is wrong, but what is correct?
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.'
); Here are the changes I've made so far, for reference. Thanks for your help! |
in discussing with veekas, i wasn't sure how to do this one either. there are two questions here.
my best guess is to stick the test inside of the |
Can't you check
Not sure what you mean. The original direction by @veekas makes sense to me. Test it in React itself (not some component's methods) right after the instance is created. In fact you can find where React calls the constructor in this file, and check right after it. |
Hey @veekas, are you still working on this? I'd like to try finishing it. |
He's almost done with it afaik. look at his PR |
Oh wow, completely forgot about this. Thanks for the ping @diegoborda. I'll have it done asap. |
Hey guys, is this still in progress? would be happy to take it. |
@benbakhar If you'd like, please take a look at #11658 and review it if you see room for improvement! Same goes for @diegoborda, @isaacchien, et al. If any further changes are requested by @gaearon or other contributors, I'd love to pass this baton to one of you. |
Sorry we dropped this. The past months have been pretty intense. :-) |
Fixed in #11658 |
Thanks! No worries, glad to officially have done a little bit to make React better. =) |
See this example: #11508 (comment).
I think it probably reflects a misunderstanding of how props and state work, and we should detect and warn if we see this.
The text was updated successfully, but these errors were encountered: