Replies: 3 comments 22 replies
-
You want to check #620 and #635 by @sarimarton, if you haven't. They are so insightful. Also, #609 is an idea from mobx. tldr; I like your work on these experiment, and encourage you to work as a third-party library, in valtio ecosystem. Valtio exists for the separation of state and snapshot. My invention here is, the separation fits well with React immutability model (and for useSyncExternalStore API), and it separates the concern of implementation to make the code more maintainable.
That's actually convincing my philosophy. valtio only cares about "set" handler, and proxy-compare only cares about "set" hanlder. This separation, I believe, simplifies things. And, The original intention of valtio isn't a replacement for mobx, but more to position itself different from mobx. But, now we see several people want it as the replacement, which is good. |
Beta Was this translation helpful? Give feedback.
-
I had seen that before, but re-reading now with a bit more experience was very useful; thanks!
Well, that makes me not feel as bad for flailing about. :-)
True, although fwiw I believe the way React's useStates work internally, a dirty-check against a B/c my reading of the reactwg link was that all setStates are magically queued to "the next render", and so the
Unfortunately I believe any dirty check during I.e. see one of their initial semi-user-land/semi-native implementations of facebook/react@06f98c1#diff-cfa128bca4d5b386950dc89bf856c5b071a8a081cf7edce12802a415eef1e72dR1327 And then this comment which AFAIU says "any So, I haven't pulled up your latest I have bothered @dai-shi about this several times (albeit never this succinctly :-), but I believe the huge "miss" with I.e. per the reactwg "useState is magically level 3 b/c mutations are pushed to 'the next render'", React has not exposed anyway for 3rd party state libraries to participate in this "current render / next render" capability. If they did, i.e. by passing a "render count" / "frame number" to Which is exactly what useStates do, and is exactly what's required for Level 3. Let the stale but consistent render finish. I'm just really surprised the React core team missed this--like before, the AFAICT, until React/ @dai-shi sorry to keep bugging you about this, but if you have friends/connections on the React core team, I'd be interested in their thoughts (and your thoughts as well!) on this |
Beta Was this translation helpful? Give feedback.
-
Relevant issue: facebook/react#25191 |
Beta Was this translation helpful? Give feedback.
-
Hi @dai-shi ; per other discussions, I've been experimenting with how we'd adopt Valtio in our current mobx app.
And, in particular, the ergonomics of "when do I pass a snapshot / when do I pass a store", which I agree the current best practice afaiu is your suggestion of:
After thinking about, what I like about mobx is not having this dichotomy between "the read instance" + "the write instance" + "you pass them both around".
So I've experimented a little bit with a
useStore
hook that still does usage-based re-rendering of a component, but tracks accesses to the store directly, without introducing the snapshot as "a new/different identity" that users have to juggle/reason about.I'm at the point where I'll share the experiments, and if you're interested, I'd be happy to keep iterating on the core ideas, but also I'm fine if you consider them out-of-scope for Valtio.
I have two branches, the 1st
useStore
is "pretty clean code" but usesaffectedToPathList
in a "not just debugging" manner to impl a non-snapshot-based version ofisChanged
:https://github.com/pmndrs/valtio/compare/main...stephenh:valtio:add-use-store?expand=1
And the 2nd branch is "very! ugly code" and makes super hacky/proof-of-concept changes to both proxy-compare (there is a local fork of proxy-compare in the branch) + vanilla.ts to make the
hasDirtyReads
calc as fast/efficient as possible.https://github.com/pmndrs/valtio/compare/main...stephenh:valtio:add-use-store-2?expand=1
The two hacky changes are:
proxy-compare
to take anonAccess
callback instead of anaffectedMap
so that we can record each access directly in our data structure, instead of having to recover them later viaaffectedToPathList
Op
to include the original/most-leaf store so that the root subscriber knows not just the path ofbooks/0/title
but also has thebooks/0
instance itselfNote that I'm not at all proposing these proxy-compare/vanilla.ts hacks land as is, they're merely concept/discussion stage to drive the
useStore
experiment.In terms of "does this belong in Valtio?", my pitch would be:
I think the 2nd branch in particular "checks all the boxes" in terms of great ergonomics (imo!) + usage-based rendering + changes the identity of proxies on change so that deps/useMemo still "just work" as users expect (which is a great feature of snapshots &
useSnapshot
)While the "separation of snapshot+store" is today fundamental to Valtio, and admittedly many React state libraries/React itself, personally I'd pitch that, as a "proxy state library", it'd be within the mission statement to provide a "single proxy identity" solution, given my admittedly biased assertion that that is why many users of "proxy based" state libraries use them in the first place.
But totally your call!
I've already spent way more time than I'd wanted, so if you're not interesting in the
useStore
prototypes, that's actually not a bad thing b/c I can go back to "real work". :-DBeta Was this translation helpful? Give feedback.
All reactions