-
Notifications
You must be signed in to change notification settings - Fork 558
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
Sync state #9
Sync state #9
Conversation
Can you revert your first commit (a0a8563) please? I think they’d like to keep the template for future RFCs. |
revert This reverts commit cc3ec12.
@j-f1 what will happen with rfc doc I created then in that case? (I am not very seasoned in git) |
Async state one of the major performance boosts in the React, When I asked about this in the past my request was rejected from Dan Abramov by explaining to me how should I correctly update the state, then I didn't faced any problems with the state management |
I'd like to push back on this RFC just a bit. Async state is a huge performance boost to React overall. A synchronous state setter would block the thread, and that's not something we really want to do. You may have many other components competing for resources at the same time and being able to defer computation is a big win. I will agree that Lastly, not many computations in |
referring to setState callback which @kyleshevlin mentioned.
Say you want to get value on console but are interested in that only when you assigned 1 to value - hence the callback. What will be output in your opinion of above code? answer: 2. So is this not confusing that user might have thought that hey in Isn't that confusing? Maybe those are the kind of things OP referred to. |
@David-Jam I meant that you could nest another setState within your callback. The way you have written that, someone who knows and understands React would know that the objects |
@kyleshevlin then it is a bit confusing why tie a callback with a particular function invocation if that callback might not be called when the function change takes effect e.g. like with |
@David-Jam Using setState callback should not be naive. It's advanced feature which is not required in regular code (I used it twice in large applications). |
@TrySound so you say it is not possible to arrive at a gotcha situation (e.g. situation or needs where it would be difficult to architect your app as to manage state as you want) with current approach of async react state management? (that you would not arrive with a sync state). I am asking out of curiosity not criticizing someone or smth. |
@David-Jam I wouldn't say it's difficult, however it requires some knowledge about how API works. That's I meant as "not naive". Just follow rules and nobody will get hurt. |
@TrySound even if you know how API works I think it is beyond doubt that it is easier to manage app state when that is synchronous. |
Easier !== better. Programming is hard AFAIK. But if you know things you CAN work with them. |
@TrySound of course easier is better in this case, why introduce unnecessary complexity? Anyway we are heading in a different direction now so I will finish of my discussion here with this comment. |
Here's the thing. It's necessary complexity for future react features. |
The author of this RFC is apparently now a @ghost. Does this have any bearing on the RFC process? |
It shouldn’t affect the initial review but if we don’t reject it and ask for a revisions then we’d need somebody to take over (assuming the author doesn’t want to). |
@gaearon I guess there's nobody agreed with the author since there's a proposal from you and other react maintainers about async rendering. |
I wrote a bit about why React works this way: facebook/react#11527 (comment) |
We have used something like React and something like Redux, but with sync API. What was wrong?
Sooner or later executing point will reach the end of the initial setState call, and run some code after it. The only problem - you just time-travelled back and doing something inside a component which does not reflect the reality. And you may face 2-3-4-5 past versions of yourself. Loopers, you know. So please don't destroy the universe, and use async version of a setState. And let the Event Horizon protect you. |
To sum this up:
Therefore, closing. |
Add a new method which will allow to synchronously do a set state, e.g.
this.setStateSync({val:x})
or replace old asynchronous method with this one.
more details in the RFC.
PS. I apologize, due to some circumstance change, I might not be able to keep an eye on RFC but I will still leave it here, maybe someone will make a new pull request out of it and keep working with that.
PPS. I plan to cancel my account and since I am not sure if that will also delete my pull request I will leave a copy of the RFC here too.
I apologize if I caused inconvenience.
Start Date: (2017-12-15)
RFC PR: (leave this empty)
React Issue: (leave this empty)
Summary
Add support for synchronous state in React: (1) Either replace the existing asynchronous setState method, or (2) Add a new function which should synchronously set state.
Basic example
this.setState({val: newVal}) // Now react should synchronously update state instead of asynchronously
or
this.setStateSync({val: newVal}) // Now react should synchronously update state instead of asynchronously
Motivation
The motivation is that it is much easier to reason about application state when the function which modifies state does this synchronously.
Nobody will disagree that react's asynchronous setState has caused lot of confusion among developers.
Even given the solution react has - like setState with a function(which gives access to a so called "pending state"), still doesn't prove that there can't be any gotchas (or edge cases) when managing state asynchronously.
Ask people why are they using mobx for example.
Drawbacks
Honestly I can't think of any drawbacks which would outweigh the advantages that synchronous setState would have.
Adoption strategy
If you directly change the existing setState with a synchronous one this seems like a breaking change, if you add a new synchronous setState function this doesn't seem to be a breaking change.
How we teach this
It will be much easier to teach about it than the existing asynchronous setState.