Skip to content

I need an explanation for Svelte component rendering. #13098

Answered by brunnerh
0x1618 asked this question in Q&A
Discussion options

You must be logged in to vote

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…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@0x1618
Comment options

@brunnerh
Comment options

Answer selected by 0x1618
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants