You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Often, I find myself wanting to "mix-in" some additional properties based on the result of a $state (array or object). For primitives, $derived works great. However, for arrays/objects, doing something like this will wreck reactivity:
const person = $state([])
const address = new SvelteMap<String,Address>()
const combo = $derived.by(()=> person.map(p=>({...p, address.get(p.name)}))
The problem lies with the fact that for any change in either person or address will correctly cause combo to react. However, this creates a new combo object (via assignment) each time. Even if this combo object is iterated using a keyed #each block in the component, the entire block of elments is onmounted and remounted.
This is deceptive as the docs around keyed blocks makes it sound like the id is the identity of the object, however, object identity is actually used, at least in these cases.
Furthermore, its really not easy to squeeze myself in with my own "view" of this combined model, since I need to return the underlying $states to the component for it to work. In my case, I know exactly when I update person so I can go ahead and create my own parallel structure of combo as a $state. But I'll have to take care of looking at the previous state, applying the changes, and then only updating the differences in place to a make things properly react without looking object identity. This is a PITA. I would like to create my own Proxy that could take care of this for me, but I think svelte is in a much better position to do this for me.
It would be nice to have a new primitive for $projected or similar, where input would be the oldvalue and the result would perform a delta update, and trigger appropriate reactivity.
Its almost like; give me a way to create my own proxy that plays with svelte internals; give me the tools to create my own SvelteMap or similar.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Often, I find myself wanting to "mix-in" some additional properties based on the result of a
$state
(array or object). For primitives,$derived
works great. However, for arrays/objects, doing something like this will wreck reactivity:The problem lies with the fact that for any change in either
person
oraddress
will correctly cause combo to react. However, this creates a new combo object (via assignment) each time. Even if this combo object is iterated using a keyed#each
block in the component, the entire block of elments is onmounted and remounted.This is deceptive as the docs around keyed blocks makes it sound like the id is the identity of the object, however, object identity is actually used, at least in these cases.
Furthermore, its really not easy to squeeze myself in with my own "view" of this combined model, since I need to return the underlying
$state
s to the component for it to work. In my case, I know exactly when I updateperson
so I can go ahead and create my own parallel structure of combo as a$state
. But I'll have to take care of looking at the previous state, applying the changes, and then only updating the differences in place to a make things properly react without looking object identity. This is a PITA. I would like to create my ownProxy
that could take care of this for me, but I think svelte is in a much better position to do this for me.It would be nice to have a new primitive for
$projected
or similar, where input would be theoldvalue
and the result would perform a delta update, and trigger appropriate reactivity.Its almost like; give me a way to create my own proxy that plays with svelte internals; give me the tools to create my own
SvelteMap
or similar.Beta Was this translation helpful? Give feedback.
All reactions