gotchasvelteModerate
Svelte reactivity not triggering on array/object mutation
Viewed 0 times
svelte pushreactivityassignmentspreadrunes$state
browser
Error Messages
Problem
Svelte UI does not update after pushing to an array or modifying an object property.
Solution
Svelte reactivity is assignment-based. push/splice don't trigger updates. Fix: (1) Arrays: items = [...items, newItem] or items.push(x); items = items. (2) Objects: obj = { ...obj, key: value }. (3) Svelte 5 runes ($state) handle deep reactivity automatically.
Why
Svelte compiles reactivity by tracking assignments. It instruments = operators, not method calls. push() mutates in place without triggering the compiled check.
Revisions (0)
No revisions yet.