-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Feature request: warn when this.state is touched directly rather than with setState #2272
Comments
This commit changes this.state to this._state so that the user never really touches this._state directly without being warned. It uses ES5 Object.defineProperty for catching the `set` operation. A test is also added for that. Closes facebook#2272
Hi, I'd like to take a look at this. |
…f this.setState(). Fixes facebook#2272.
…y of the current state when running in dev mode. facebook#2272
Hey, this bug seems to be open since long. I got some free time, can I try this? |
Hey @kaushik94! Feel free to give it a shot! There was a previous PR open for this that you should check out: #2283. Read through the comments to get a good idea of how to approach this 👍 Feel free to ping me if you have any questions! |
Hi. I have an open pull request (#8265) that addresses this issue in the way described by @sebmarkbage in #2283. It's just waiting on a code review. |
Thanks for reminding me @iansu, I missed it as the PR didn't link to this issue (I'll add a mention to the top comment for you). I'll try and make sure it gets reviewed soon! |
React currently warns if We do not detect or warn about direct modifications to state properties though (eg So I'm going to close this issue. 😄 |
I've just stumbled across some code written which touches this.state directly and then forces an update. The danger in this pattern is that appears to work, it still leaves the code vulnerable to other callers of setState doing the right thing, being queued up, and then overwriting the hand-touched state, if I'm understanding the red box on http://facebook.github.io/react/docs/component-api.html correctly.
Any chance if, in (at least) the dev version, this.state could be guarded with a JS setter that would print a warning?
The text was updated successfully, but these errors were encountered: