I'm reading the section about reactivity and the way svelte handles it inside each blocks.
this is the example to show how svelte does NOT update the referenced array when items are primitive values:
<script>
let list = [1, 2, 3];
</script>
{#each list as item}
<button on:click={() => item *= 2 }>{ item }</button>
{/each}
this is a possible workaround that could be added to the recipe: (check the repl)
<script>
let list = [1, 2, 3];
const updateList = (index) => list[index] *=2
</script>
{#each list as item, index}
<button on:click={() => updateList(index) }>{ item }</button>
{/each}
Moreover, doing some tests I found a strange beahviour regarding svelte and arrays with primitive values. If I try to assign an item which is a primitive value from inside an each block, just like the example above, the array reactivity is broken, it seems like it changes the array reference to something else.
Here's is a repl reproducing this behaviour. As soon as you click one button of the first group, the array will no longer be updated.
I guess that svelte should try to issue a warning when this happens, updating an item which is a primitive value from an each block should not be allowed if it carries this nasty side effect. Do you think I should create an issue at svelte for such a thing?
BTW: great initiative this recipes repo, it's really bery useful, and the reactivity section was very much needed indeed.