Referencing elements inside a writable List #14003
Replies: 1 comment
-
It's not a copy and this is not about whether a reference or copy is used either.
Stores do not have fine-grained reactivity, so any change to some contents of the store has to also signal those changes to the store itself for reactivity to work. (If you reference the store directly or indirectly via an So if your goal is that the store is still notified, you could either change the contents of the store to be a full state proxy from the top down (wrapping the entire array) or manually trigger the store via I would recommend completely avoiding stores if you can and work directly with Also, please do not cross-post, it wastes people's time. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have the following list of
Player
objects{id:number, name:string}
which is a globally stored element made usingwritable
:Now, in my main
svelte
file I want to reference the first player usingplayer = $state($players[0])
, but this only creates a copy, not a reference of the first element in$players
. However, what confuses me is that$players[0]
is a reference of this first player, and when I call a player variable with{#each $players as player}
it's also a reference.This video shows exactly what I mean:

The button on the left is changing the variable
player
but doesn't trigger any updates on$players
and the button on the right does the exact opposite. My question is how do I makeplayer
be a reference instead of a copy.Beta Was this translation helpful? Give feedback.
All reactions