-
Hello, today I started looking into Svelte, and it seems very cool, but I'm not sure if I fully understand how Svelte works. My doubts began with Keyed each blocks, where Svelte doesn't delete the entire component but only a part of it. Why doesn't it delete the whole component DOM element, but only a part of it? Why are other elements affected, and why do their emojis change to something different? I don't understand this, so I would really appreciate an explanation of how Svelte's rendering works. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This is specific to There is no universal way of identifying items in an array, so by default Svelte assumes that items stay in place and are only added/removed at the end. If a component is used in So e.g. if the first item is removed, the existing component is updated with the data from the second item which takes the place of the first item. If a component has any local state which is not reactively dependent on the component props, this will cause desynchronization (as with the A keyed |
Beta Was this translation helpful? Give feedback.
This is specific to
#each
, the problem is item identity.There is no universal way of identifying items in an array, so by default Svelte assumes that items stay in place and are only added/removed at the end. If a component is used in
#each
, using other operations (like moving items or inserting/deleting elsewhere in the array) can cause issues, since then the assumption is wrong and the only thing Svelte "sees" is the data of an item changing.So e.g. if the first item is removed, the existing component is updated with the data from the second item which takes the place of the first item.
If a component has any local state which is not reactively dependent on the component props, this w…