Replies: 1 comment 2 replies
-
Hi, thanks for opening a discussion.
It seems to me that it's a doc issue. We should basically use the root state. - const users = useSnapshot(state.users);
+ const { users } = useSnapshot(state); That way, it works intuitively, no? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
One of the big gotchas that I've run into several times with Valtio is that you must deeply mutate the state in order to guarantee compatibility with all
useSnapshot
use cases. If a snapshot was taken of a slice of the proxy state, and that proxy state is reassigned instead of mutated, any observers on that state break.One of the major benefits of the mutable state model is that it is, in theory, easier to update state than with an immutable model because you don't have to ensure that new references are created. However, currently Valtio has the opposite problem where extra care has to be taken to ensure the new references are never created. This is particularly clunky when you do want to entirely replace a chunk of complex state - it requires first mutating the state back to some default version, and then mutating it with the new values.
It's not hard to find examples where people have run into this issue:
#525
#76
#241
#875
I also created a codesandbox to illustrate some of these possible cases: https://codesandbox.io/p/devbox/dsh49s
In an ideal world, all of these cases would work. The case with reassignment but then also mutating the old state seems particularly unintuitive.
I wonder if this is something that could be considered as part of v2? It would indeed be a breaking change, but also seems like it would address one of the big pain points and sources of confusion with Valtio.
Beta Was this translation helpful? Give feedback.
All reactions