← Back to React, page 7

react-0122 · Medium mid

What is structural sharing, and how do immutable updates help React skip work?

Structural sharing means that when you create a new version of a nested object, you copy only the path to the changed value and reuse (share) every unchanged branch. Reused branches keep the same reference, so React can skip them with a cheap equality check (memo, dependency arrays, selectors). Spread syntax does this by hand, and libraries such as Immer do it automatically. Mutation breaks it, because the reference is unchanged although the content changed.

Full answer

React compares values by reference (Object.is) to decide whether things changed: props in memo, dependencies in useEffect and useMemo, and selectors in stores. This only works if you never mutate objects in place and always create a new object when something inside changes.

Copying everything on each update would be expensive and would defeat memoization, because every branch would get a new identity. Structural sharing solves both. An update such as "rename the student" creates a new root object and a new student object (the path to the change), but the courses array and the address object, which did not change, are reused. Their references are identical to the previous state, so components that only depend on them can skip rendering.

By hand with spread: { ...state, student: { ...state.student, name: 'Meera' } }. This is correct and explicit, but deeply nested updates become verbose and easy to get wrong.

With Immer, used inside Redux Toolkit's createSlice, you write the update as if you were mutating a draft (draft.student.name = 'Meera'), and Immer produces the new immutable state with structural sharing. It records only the touched paths and reuses the rest. If nothing changes, it returns the same state object. Immer also freezes the result in development to catch accidental later mutation.

Implications. Selectors that return the same reference for unchanged data let useSelector avoid re-renders. memo children with unchanged object props are skipped. Time-travel debugging and undo become easy because old versions remain intact. The cost is allocation and a different way of thinking, which is small compared with the rendering work it saves.

Pitfalls: mutating nested objects after copying only the top level (shallow copy), using JSON.parse(JSON.stringify(x)) for deep clones (this destroys sharing and makes every branch new), and copying large arrays on every keystroke without need.

The example shows that unchanged branches keep their identity.

jsx
import { produce } from 'immer';

const state = {
  student: { name: 'Asha', age: 20 },
  courses: ['Maths', 'Science'],
  address: { city: 'Pune' },
};

const bySpread = { ...state, student: { ...state.student, name: 'Meera' } };
console.log('spread: student is new: ' + (bySpread.student !== state.student));
console.log('spread: courses is shared: ' + (bySpread.courses === state.courses));
console.log('spread: address is shared: ' + (bySpread.address === state.address));

const byImmer = produce(state, (draft) => {
  draft.student.name = 'Meera';
});
console.log('immer: student is new: ' + (byImmer.student !== state.student));
console.log('immer: courses is shared: ' + (byImmer.courses === state.courses));
console.log('immer: address is shared: ' + (byImmer.address === state.address));
console.log('immer: original untouched: ' + state.student.name);

const unchanged = produce(state, () => {});
console.log('immer with no changes returns the same object: ' + (unchanged === state));

const cloned = JSON.parse(JSON.stringify(state));
console.log('JSON clone shares courses: ' + (cloned.courses === state.courses));

This ran as a script with the Immer library, the same one that Redux Toolkit uses inside createSlice. Output of the run: spread: student is new: true spread: courses is shared: true spread: address is shared: true immer: student is new: true immer: courses is shared: true immer: address is shared: true immer: original untouched: Asha immer with no changes returns the same object: true JSON clone shares courses: false

Key points

  • React skips work by comparing references, so updates must create new objects instead of mutating.
  • Structural sharing copies only the changed path and reuses unchanged branches.
  • Spread syntax does it by hand. Immer does it automatically from mutation-style code.
  • Unchanged references let memo, selectors and dependency arrays skip work.
  • Deep cloning with JSON destroys sharing and should be avoided.

Common mistakes

  • Copying only the top level and mutating nested objects.
  • Deep cloning with JSON for every update.
  • Mutating state and expecting memoized components to update.
  • Creating new arrays or objects for data that did not change.
  • Expecting Immer drafts to work outside the produce callback.

Follow-ups

  • How does Immer know which parts of the state were changed?
  • Why do selectors depend on stable references?
  • When would you avoid Immer and write spread updates?

Tips

  • Define structural sharing in one sentence before showing code.
  • Connect it to memo and selector behaviour.
  • Warn against JSON deep clones.