0
0
Svelteframework~5 mins

Array and object reactivity gotchas in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
Why does directly modifying an array element like arr[0] = 5 not trigger reactivity in Svelte?
Svelte's reactivity tracks assignments to variables, not mutations inside arrays or objects. Changing arr[0] does not reassign arr, so Svelte does not detect the change automatically.
Click to reveal answer
beginner
How can you make Svelte react to changes inside an array after modifying an element?
After changing an element, reassign the array to itself like arr = arr. This tells Svelte the variable changed and triggers updates.
Click to reveal answer
intermediate
What is a common gotcha when updating properties inside an object in Svelte?
Modifying a property like obj.prop = value does not trigger reactivity because the object reference stays the same. You must reassign the object, e.g., obj = {...obj}, to notify Svelte.
Click to reveal answer
intermediate
Why is using methods like push() or splice() on arrays tricky in Svelte reactivity?
These methods mutate the array in place without changing its reference. Svelte won't detect changes unless you reassign the array after mutation.
Click to reveal answer
advanced
What is the recommended pattern to update nested objects reactively in Svelte?
Create a new object with updated nested properties using spread syntax, then reassign the main object. For example, obj = {...obj, nested: {...obj.nested, key: value}}.
Click to reveal answer
In Svelte, which action will trigger reactivity after changing an array element?
AJust change the element, e.g., arr[0] = 5
BReassign the array variable to itself, e.g., arr = arr
CCall arr.push(5)
DNo action needed, Svelte detects all changes automatically
Which method does NOT trigger Svelte reactivity by itself when used on an array?
Aarr.push(newItem)
Barr = arr.filter(...)
Carr = arr
Darr = [...arr, newItem]
How do you trigger reactivity after changing a property inside an object in Svelte?
ADirectly assign the property, e.g., obj.prop = value
BCall a method on the object
CNo need to do anything
DReassign the object with a new copy, e.g., obj = {...obj}
What happens if you mutate a nested object property without reassigning the parent object in Svelte?
ANo reactivity is triggered
BSvelte updates the UI automatically
CSvelte throws an error
DThe nested property becomes reactive
Which is the best way to update a nested object property reactively in Svelte?
ACall a function to update the nested property
Bobj.nested.key = value
Cobj = {...obj, nested: {...obj.nested, key: value}}
DUse a Svelte store
Explain why directly modifying an array or object property does not trigger reactivity in Svelte and how to fix it.
Think about how Svelte knows a variable changed.
You got /4 concepts.
    Describe the recommended pattern to update nested object properties reactively in Svelte.
    Focus on creating new objects instead of changing existing ones.
    You got /4 concepts.