-
Notifications
You must be signed in to change notification settings - Fork 16
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
Implement additional protocols for Atomified react-state #11
Comments
I'm hijacking this issue to discuss about something that caught me a little bit unawares with Atomified. I had this code:
I would expect based on my Clojure experience that I tried to work around this by relying on the return value of I now wonder if the semantics of a Clojure atom make sense for this rendering lifecycle that React uses, where the new state is only available after a re-render. |
A workaround for this is to manually update the deref-ed value, and then reset! it into the new state to trigger the rendering. So something like this:
|
Hm. Yeah, useState is more like an agent than an atom, since updates are scheduled by React asynchronously. And it schedules the update itself, not the value, so it's impossible to return the correct value from swap! without bugs. |
I think that we want to maintain the semantics of The question then is: do we drop the atom interface? |
Good point. I like the ergonomics of seeing a deref in the code, and the usual atom stuff do look convenient. But, it does seem like a leaky abstraction. Using the plain useState isn’t that painful, and the semantics are actually less surprising. So perhaps dropping the atom interface is best at this point. Or at least documenting this with great big letters. |
Re-reading this, I think @Lokeh you are up to something by saying "useState is more like an agent". I've never used Agents before, but reading the documentation I think it's definitely possible to expose this kind of semantics (or the subset that makes sense). So for example instead of using the Deref is still a thing with agents, but you get whatever the current value is, because the update happens on a different thread outside of your control. |
I find myself wondering exactly what sort of bugs one might find when using ordinary atom semantics.
It looks to me like one could adopt plain atom semantics, with the caveat that it is unsafe to reset/swap the atom during the render phase? This would be similar to allowed behaviour in class components:
One could have something like |
The issue I had was that I was assuming that inside the click handler I could access the new atom state:
Had What I am proposing is something like:
The result would be exactly the same -- Ideally one could also attach a validator to catch invalid states but that's a second step. |
yes - I understand the use-case; my question is, why not provide a state API such that |
There are subtle interplays with useEffect that Lokeh has found out. We should collect all this discussions into a single page because it’s spread out over many issues and PRs etc. |
Closing this for now. What I've done is kept The |
IWatchable, IEquiv, IHash, IPrintWithWriter would all be nice to have
The text was updated successfully, but these errors were encountered: