sveltejs / svelte

web development for the rest of us
https://svelte.dev
MIT License
79.71k stars 4.23k forks source link

Svelte 5: "ownership_invalid_binding" with arrays #13137

Closed dm-de closed 1 month ago

dm-de commented 1 month ago

Describe the bug

I'm unable to get rid of "ownership_invalid_binding" warning, which happens - for example - if I slice an array.

Look for Table.svelte in bottom example. Table has a pagination inside - and it is required to slice the array. So I use $effect to create new sliced array.

Reproduction

It shows warnng "ownership_invalid_binding" on start. If you click pages 1 or 2. You will see more of them.

LINK

Logs

No response

System Info

svelte@5.0.0-next.243

Severity

annoyance

paoloricciuti commented 1 month ago

My immediate suspicion is that slice is updating the length source inside the array...since the length source has been created in App.svelte (at the moment of the array creation) and you are binding the array but not length this might cause some problem...will investigate this a bit later.

trueadm commented 1 month ago

So the issue here is these lines

$effect(() => {
  rows2 = rows.slice((page - 1) * limit, page * limit)
})

It messes with the ownership logic because rows2 might belong to Table.svelte, so it can be bound through as a prop, but all its contents are owned by App.svelte – so the ownership logic is muddled up. If you pass through bind:rows={rows}, things work fine again.

I'm honestly unsure how to approach this one and @Rich-Harris and @dummdidumm have more context here, so I'll defer to them.