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 if this.state is set to this.props referentially #11593

Closed
gaearon opened this issue Nov 18, 2017 · 14 comments
Closed

Warn if this.state is set to this.props referentially #11593

gaearon opened this issue Nov 18, 2017 · 14 comments

Comments

@gaearon
Copy link
Collaborator

gaearon commented Nov 18, 2017

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.

@swyxio
Copy link
Contributor

swyxio commented Nov 18, 2017

Happy to take!

edit: instead of doing this myself i am working with @veekas to get him started contributing to react, below

@gaearon
Copy link
Collaborator Author

gaearon commented Nov 18, 2017

Sounds good.

@veekas
Copy link

veekas commented Nov 19, 2017

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 this.state = props. Do you have any tips?

I currently am looking at putting the warning in ReactFiberClassComponent.js. Something like the following:

// 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!

@swyxio
Copy link
Contributor

swyxio commented Nov 20, 2017

in discussing with veekas, i wasn't sure how to do this one either. there are two questions here.

  1. how to test if this.state is being set to React.Component.props
  2. -where- to test it

my best guess is to stick the test inside of the componentWillMount lifecycle method execution as that is the earliest place we can check this.state.

@gaearon
Copy link
Collaborator Author

gaearon commented Nov 20, 2017

Can't you check instance.state === instance.props? I don't understand what parent is or how it is relevant.

my best guess is to stick the test inside of the componentWillMount lifecycle method execution as that is the earliest place we can check this.state

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.

@isaacsolo
Copy link

Hey @veekas, are you still working on this? I'd like to try finishing it.

@swyxio
Copy link
Contributor

swyxio commented Feb 5, 2018

He's almost done with it afaik. look at his PR

@diegoborda
Copy link

diegoborda commented Mar 14, 2018

Hi! I see this issue is taken but it seems still. @sw-yx @veekas Are you taking it? I can finish it if you want.

@veekas
Copy link

veekas commented Mar 14, 2018

Oh wow, completely forgot about this. Thanks for the ping @diegoborda. I'll have it done asap.

@benbakhar
Copy link

Hey guys, is this still in progress? would be happy to take it.

@veekas
Copy link

veekas commented Aug 28, 2018

@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.

@gaearon
Copy link
Collaborator Author

gaearon commented Aug 28, 2018

Sorry we dropped this. The past months have been pretty intense. :-)

@gaearon
Copy link
Collaborator Author

gaearon commented Aug 28, 2018

Fixed in #11658

@veekas
Copy link

veekas commented Aug 28, 2018

Thanks! No worries, glad to officially have done a little bit to make React better. =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants