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

Feature request: warn when this.state is touched directly rather than with setState #2272

Closed
dmose opened this issue Sep 29, 2014 · 8 comments

Comments

@dmose
Copy link

dmose commented Sep 29, 2014

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?

cirocosta pushed a commit to cirocosta/react that referenced this issue Oct 2, 2014
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
@cirocosta
Copy link

Hi @dmose ! This seems like a great thing! I've made PR trying to address it (see #2283). I'd appreciate to hear what you think!

@iansu
Copy link
Contributor

iansu commented Nov 10, 2016

Hi, I'd like to take a look at this.

iansu added a commit to iansu/react that referenced this issue Nov 10, 2016
iansu added a commit to iansu/react that referenced this issue Jan 14, 2017
@kaushik94
Copy link

Hey, this bug seems to be open since long. I got some free time, can I try this?

@aweary
Copy link
Contributor

aweary commented Feb 3, 2017

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!

@iansu
Copy link
Contributor

iansu commented Feb 3, 2017

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.

@aweary
Copy link
Contributor

aweary commented Feb 3, 2017

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!

@kaushik94
Copy link

@aweary I just skimmed through @iansu 's PR and it looks great to me, I don't have to work on this I guess. @aweary can you point me to any issue that I can help with ? All the easy to solve issues has someone already working. Thanks :)

@bvaughn
Copy link
Contributor

bvaughn commented Mar 27, 2017

React currently warns if this.state is replaced- because it's not the recommended way to update state and may mask potential bugs. However we still support it (and will likely continue to support it).

We do not detect or warn about direct modifications to state properties though (eg this.state.foo = 'bar'). I think this is okay. I don't think it would be 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 issue. 😄

@bvaughn bvaughn closed this as completed Mar 27, 2017
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

8 participants