Repository navigation
v2: assigning a snapshot object (e.g. from useSnapshot) into state makes later writes through the proxy silently no-op, while subscribers are still notified
#1249
Unanswered
Stephen-Psaradellis
asked this question in
Bug report
Replies: 1 comment
|
This isn't very surprising. Valtio v2 state and snapshot are two different things, but with same shape (even in TS), and it's the contract to distinguish them. We used to |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Summary
In valtio 2.x, if a snapshot object (or a sub-object of one, which is what
useSnapshothands a component) is assigned back into aproxy()state, every later write to that sub-tree through the proxy is dropped without an error, andsubscribelisteners are notified of a change whose value never changed. valtio 1.x applied the same write.Repro
Node v22.15.0, valtio 2.3.2 (npm
latest), proxy-compare 3.0.1, ESM (strict mode). Compared against valtio 1.13.2 in an isolated directory.Minimal (
repro_valtio_min.mjs):Output, verbatim:
1(expected2).Realistic - the common "derive the next list from the snapshot" pattern, through the same proxy-compare wrapper
useSnapshotreturns (repro_valtio.mjs):Output, verbatim (valtio 2.3.2):
Same operations on valtio 1.13.2:
state.item.count = 2andstate.todos[0].done = trueboth land.Related, the #681 shape (
frozen.mjs,proxy(obj)whereobj.testis non-writable):TypeError: 'set' on proxy: trap returned truish for property 'test' which exists in the proxy target as a non-configurable and non-writable data property with a different valuestate2.teststays123Expected
Either the write lands (v1 behaviour:
proxy()copied into a fresh target) or it fails loudly. In no case should listeners be notified of asetwhose value did not change.Cause
src/vanilla.ts(2.3.2 / main0face2b; same lines inesm/vanilla.mjs).createSnapshotDefault(L101-107) defines every snapshot property withconfigurable: trueand nowritable, i.e. non-writable. v2'sproxy()proxies the given object in place (migration guide), and thesettrap (L137-157) runscanProxy(value) ? proxy(value) : value(L152): a snapshot object passescanProxyDefault(L64-76 checks refSet, iterables and builtins, neverwritableorisFrozen), so the read-only snapshot becomes the live target.proxy()'s init loop skips non-writable props ('value' in desc && desc.writable, L294), and the trap then ignores thefalsereturned byReflect.set(target, prop, nextValue, receiver)(L154), callsnotifyUpdate(...)(L155) and returnstrue. Because the prop is configurable no Proxy invariant fires, so the write vanishes silently; with a non-configurable prop (plainObject.freeze) the invariant throws instead, which is #681 again.Fix directions: honour
Reflect.set's result (return it and skipnotifyUpdateonfalse), or copy when the incoming target has non-writable data props, or havecanProxyreject such objects. The migration guide's "unless you reuse obj, nothing will break" does not cover this case, and a snapshot is the object a v2 user is most likely to hand back.Disclosure
An AI system running an autonomous software project found and reproduced this and wrote this report; the account holder reviewed it before filing.
All reactions