Skip to content
Learn Netverks

Lesson

Step 17/36 47% through track

immutability-patterns

Immutability patterns

Last reviewed May 28, 2026 Content v20260528
Track mode
client_react
Means
In-browser React TSX
Reading
~1 min
Level
advanced

This lesson

This lesson teaches Immutability patterns: the concepts, APIs, and habits you need before advancing in React.

Without Immutability patterns, you will struggle to read or extend React codebases and playground exercises.

You will apply Immutability patterns in contexts like: SPAs, dashboards, design-system-driven products, and React Native mobile apps.

Write TypeScript/TSX, click Run in browser—React 18 loads from CDN, JSX compiles in the tab, UI renders in the preview root, and printOutput feeds the terminal.

When hooks, state, and effects from intermediate lessons are familiar.

React state updates must be immutable: never mutate arrays or objects in place. Create new references so React detects change and re-renders predictably.

Array updates

  • Add: [...items, newItem]
  • Remove: items.filter((x) => x.id !== id)
  • Update one: items.map((x) => x.id === id ? { ...x, done: true } : x)

Object updates

Shallow copy with spread: setUser((u) => ({ ...u, name: 'New' })). Nested structures need copying each level you change.

Important interview questions and answers

  1. Q: Why not push to state array directly?
    A: Mutating the same reference may skip re-render and breaks time-travel debugging tools.
  2. Q: immer or immutable libraries?
    A: Optional sugar for complex nested updates; spread/filter/map are enough for most app code.

Self-check

  1. How do you toggle a todo’s done flag immutably?
  2. What is wrong with items.sort() on state directly?

Challenge

Toggle todo

  1. Click a todo label to toggle done.
  2. Confirm completed items get line-through style.

Done when: each todo toggles independently without mutating the array.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Mutation bug example?
  • Spread vs structured clone?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump