Svelte Medium technical 1 views 1 min read

Why does mutating arrays or objects sometimes not update a Svelte 3/4 component?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of Svelte conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

In Svelte 3 and 4 reactivity is triggered by assignment to the variable the compiler tracks. Mutating an array or object in place does not notify the compiler because no assignment occurs.

<script>
  let items = [1, 2, 3];
  function add() {
    items.push(4);          // no update
    items = items;          // hacky, but triggers
    items = [...items, 4];  // idiomatic
  }
</script>

Use the spread operator or concat, filter and map to produce new arrays, or reassign properties into a new object, for example obj = { ...obj, key: value }. Nested component props also only update when the reference changes, so immutable updates are the norm.

Svelte provides $: reactive statements to recompute derived values when dependencies are assigned. With Svelte 5 $state, arrays and objects are deep proxies, so items.push(4) does update the UI. The runes model removes this common pitfall while keeping assignment intuition for primitives.

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?