Discover the simple trick that makes your Svelte app update instantly when you change arrays or objects!
Why Array and object reactivity gotchas in Svelte? - Purpose & Use Cases
Imagine you have a list of tasks in your app. You add a new task by pushing it to an array or update a task property inside an object. But the screen does not update to show your changes.
Directly changing arrays or objects in Svelte does not always trigger the screen to update. This happens because Svelte tracks changes by assignments, not by internal mutations. So, pushing to an array or changing an object property silently fails to refresh the UI.
Svelte requires you to reassign the array or object after changing it. This tells Svelte the data changed and it should update the screen. For example, after pushing to an array, you write tasks = tasks to trigger reactivity.
tasks.push(newTask); // UI does not updatetasks.push(newTask); tasks = tasks; // UI updates
This understanding lets you build dynamic apps where changes to arrays and objects always show up instantly on screen.
Think of a to-do list app where you add or check off tasks. Without proper reactivity, the list won't refresh when you update tasks, confusing users. Fixing this makes the app feel alive and responsive.
Direct mutations to arrays or objects don't trigger updates in Svelte.
You must reassign the variable after mutation to activate reactivity.
This small step ensures your UI always matches your data changes.
Practice
arr[0] = 10 not update the UI?Solution
Step 1: Understand Svelte's reactivity model
Svelte updates the UI only when a variable is assigned a new value, not when its internal parts change.Step 2: Analyze direct array element modification
Changingarr[0] = 10mutates the array but does not assign a new array toarr, so Svelte does not detect the change.Final Answer:
Because Svelte only tracks assignments to the whole variable, not internal mutations. -> Option DQuick Check:
Direct mutation ≠ UI update [OK]
- Thinking direct mutations trigger updates
- Believing arrays are immutable in Svelte
- Using methods without reassignment
Solution
Step 1: Recall Svelte's reactivity for objects
Svelte detects changes only when the whole object variable is reassigned.Step 2: Evaluate each option
obj = { ...obj, key: 'value' };uses spread syntax to create a new object and assign it toobj, triggering reactivity. Direct property assignment likeobj.key = 'value';orObject.assign(obj, { key: 'value' });mutate the object without reassignment, andobj.key = 'value'; obj.update();calls a non-existent method.Final Answer:
obj = { ...obj, key: 'value' }; -> Option AQuick Check:
Reassign object with spread = reactive update [OK]
- Mutating object properties directly
- Expecting methods like obj.update() to exist
- Using Object.assign without reassignment
let numbers = [1, 2, 3]; numbers.push(4);
What will Svelte render if you display
{numbers.join(',')} immediately after?Solution
Step 1: Understand array mutation with push()
Thepush()method changes the array but does not reassign it.Step 2: Check Svelte's reactivity trigger
Sincenumbersis not reassigned, Svelte does not detect the change and the UI remains with the old array.Final Answer:
1,2,3 -> Option BQuick Check:
Array mutation without reassignment = no UI update [OK]
- Assuming push triggers UI update
- Expecting automatic reactivity on mutations
- Confusing errors with silent no-update
let user = { name: 'Alice', age: 25 };
function birthday() {
user.age += 1;
}Why does calling
birthday() not update the UI showing {user.age}?Solution
Step 1: Analyze mutation inside birthday()
The function incrementsuser.agedirectly, mutating the object.Step 2: Check reactivity requirements
Svelte requires reassignment of the whole object variable to detect changes; direct mutation alone does not trigger UI updates.Final Answer:
Because the function does not reassign the user object after mutation. -> Option CQuick Check:
Direct mutation without reassignment = no UI update [OK]
- Thinking numeric properties are special
- Assuming await is needed for sync code
- Believing direct mutation triggers UI update
items reactively in Svelte?Solution
Step 1: Understand reactivity with arrays
Direct mutation likeitems[0] = 'new'does not trigger updates unless followed by reassignment.Step 2: Evaluate each option
items = ['new', ...items.slice(1)]; creates a new array with the first element replaced, triggering reactivity correctly. items = [...items]; items[0] = 'new'; creates a shallow copy of the array first, then mutates the first element, which is reactive because the reassignment happens before mutation and the array reference changes. items[0] = 'new'; items = items; mutates first then reassigns the same array reference, which may not trigger reactivity reliably. items = items; items[0] = 'new'; reassigns the same array reference then mutates, which does not trigger reactivity.Final Answer:
items = [...items]; items[0] = 'new'; -> Option AQuick Check:
Reassign array with spread then mutate = reactive update [OK]
- Mutating after reassignment
- Reassigning without changes
- Mutating before reassignment expecting update
Ready to prove it?
Pass the test and this pattern moves unknown
