Skip to content

Warn on direct state manipulation (#2272) #8265

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

Closed
wants to merge 6 commits into from

Conversation

iansu
Copy link
Contributor

@iansu iansu commented Nov 10, 2016

Adds a warning when setting state directly using this.state instead of this.setState().

Resolves #2272

@facebook-github-bot
Copy link

Thank you for your pull request. We require contributors to sign our Contributor License Agreement, and yours has expired.

Before we can review or merge your code, we need you to email [email protected] with your details so we can update your status.

@iansu iansu changed the title Warning on direct state manipulation (#2272) Warn on direct state manipulation (#2272) Nov 10, 2016
@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

shallowEqual(this._currentState, inst.state),
'this.state: Setting state directly with this.state is not ' +
'recommended. Instead, use this.setState().'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure how useful this warning would really be, it doesn't give you any information about what was mutated or where it happened. I know that providing those details would make this more difficult, but I don't think it provides a whole lot of benefit for the cost as it stands.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair. Adding the name of the component to the warning is relatively easy. I should have done that in the first place. Showing what was changed is definitely more difficult. If people think that's valuable then I'm willing to look into it.

@oksas
Copy link

oksas commented Jan 11, 2017

@iansu were you still working on this?

@iansu
Copy link
Contributor Author

iansu commented Jan 11, 2017

@oksas I'd like to continue working on this. I have some ideas to improve the implementation, it's just unclear if those improvements will be enough to justify including this feature. Also, for some reason I didn't get a notification that @gaearon mentioned this pull request. 😞

Possible improvements:

  • Only create a copy of the component state in dev so there's no performance impact in production
  • Add the name of the component to the warning message
  • Show what part of the state was changed and where (I'm not exactly sure how to implement this one, so any feedback would be appreciated)

@aweary
Copy link
Contributor

aweary commented Feb 27, 2017

@iansu can you resolve the merge conflicts? This LGTM, I think we can move forward with it once its mergeable.

@iansu
Copy link
Contributor Author

iansu commented Feb 27, 2017

Done. Just waiting for the tests to run on CI now.

@iansu
Copy link
Contributor Author

iansu commented Feb 28, 2017

@aweary this is done now. Not sure if you saw my previous message because I didn't explicitly mention you.

@aweary
Copy link
Contributor

aweary commented Feb 28, 2017

@iansu thanks! Can you run the ./scripts/fiber/record-tests script and commit the results please?

@iansu
Copy link
Contributor Author

iansu commented Feb 28, 2017

@aweary done! Looks like a couple of tests might be failing because of my changes. I'm not too familiar with Fiber though. Let me know if I should try and fix them.

@aweary
Copy link
Contributor

aweary commented Feb 28, 2017

@iansu sorry I'm actually not too familiar with fiber yet either, and I'm not sure if we want to implement this in fiber as well before merging. cc @gaearon for those questions

@iansu
Copy link
Contributor Author

iansu commented Feb 28, 2017

@aweary @gaearon sounds good. Is there any documentation on running the Fiber tests? The "How to Contribute" page just says to run npm test. I was a bit surprised when this first ran on CI and it ran three different sets of tests. I ended up looking at the console output there to figure out how to run the SSR tests but even that seems to have changed with the move to CircleCI. Might be a good opportunity for someone to contribute to the docs.

@bvaughn
Copy link
Contributor

bvaughn commented Mar 27, 2017

Is there any documentation on running the Fiber tests?

@iansu: Sorry for chiming in late, but you can run tests against Fiber using the REACT_DOM_JEST_USE_FIBER env flag (eg REACT_DOM_JEST_USE_FIBER=true jest). Keep in mind that some failures are expected currently. The ./scripts/fiber/record-tests will let you know if any new failures were introduced because of local changes though.

That said, I think we actually want to pass on this change. Even though we warn if this.state is replaced, we still support it (and will likely continue to support it). I don't think it's worth the additional cost of cloning and doing a shallow comparison of state after each lifecycle method just to warn about something that we actually support (even though it's not recommended).

So I'm going to close this PR. Sorry for the inconvenience! And thanks a ton for the time and effort you spent on it. 🙇

@bvaughn bvaughn closed this Mar 27, 2017
@iansu
Copy link
Contributor Author

iansu commented Mar 28, 2017

@bvaughn Well that's disappointing but I appreciate the explanation. I'll keep looking for other opportunities to contribute. 😀

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

Successfully merging this pull request may close these issues.

6 participants