Why Immutability Matters in React: Updating State Safely

Understand why React state should be updated immutably, how reference equality affects state updates, and how to copy objects and arrays safely.

Why Immutability Matters in React: Updating State Safely

Why Does Immutability Matter in React?

In JavaScript, an object or array can be changed after it is created. That is called mutation. React state, however, is easier to reason about when you treat its objects and arrays as read-only: when something changes, create a new value that describes the update.

This matters because React uses identity comparisons in important places. For example, when you set state to a value that is Object.is-equal to the current state, React can skip rendering that component's children. If you mutate an object and pass back the same object reference, React may see the same state value and skip the update. New references also help memoized components and other optimizations tell when data changed.

React does not deep-compare every nested property of your state to discover what you mutated. Immutability makes the change visible through a new reference and keeps earlier state snapshots intact.

The problem with mutating an object

Here is a state object and an unsafe update:

const [profile, setProfile] = useState({ name: "Likhon", points: 10 });

function addPoint() {
  profile.points += 1; // Mutates the existing object
  setProfile(profile); // Same object reference
}

profile and the value passed to setProfile are the same object. Since the state setter sees a value equal to the current state by Object.is, React may skip the update. Even when another update happens to cause a render, changing state in place can make the result difficult to predict.

Create a new object with spread

Instead, make a new object and copy the properties you want to keep:

function addPoint() {
  setProfile((currentProfile) => ({
    ...currentProfile,
    points: currentProfile.points + 1,
  }));
}

The updater form receives the latest state, which is useful when the next value depends on the previous value. Object spread copies the existing properties into a new object, then points is replaced with the updated number. The new object has a new reference, and the old state remains unchanged.

Updating arrays without mutation

Array methods such as push(), pop(), sort(), and splice() change the original array. For state updates, use methods that return a new array or copy the array first.

To add an item, use spread:

setTasks((currentTasks) => [...currentTasks, newTask]);

To remove an item, use filter():

setTasks((currentTasks) => currentTasks.filter((task) => task.id !== taskId));

To update one item, use map() and create a new object for the changed item:

setTasks((currentTasks) =>
  currentTasks.map((task) =>
    task.id === taskId ? { ...task, done: true } : task,
  ),
);

These updates produce a new array. In the last example, the changed task is also a new object, while unchanged task objects can be reused safely because they were not mutated.

Nested state and shallow copies

Spread makes a shallow copy. Copying the outer object does not also copy every object inside it. For nested state, make a new copy at each level along the path you change:

setProfile((currentProfile) => ({
  ...currentProfile,
  settings: {
    ...currentProfile.settings,
    theme: "dark",
  },
}));

Here both the profile and its settings object are new. Other nested values that were not changed can keep their existing references.

Does every render depend only on state references?

No. A component can render because its parent rendered, its props changed, its context changed, or its own state changed. React's behavior is broader than a single reference check. The useful rule is narrower: state setters and memoization rely on identity in places, so mutating state while reusing its reference can hide an update and cause confusing results. Treating state as immutable gives React clear new values to work with.

Try it yourself

You have this state:

const [cart, setCart] = useState([{ id: 1, name: "Notebook", quantity: 1 }]);

Write an update that increases the quantity of the item with id 1. Use the functional updater, map(), and object spread. Avoid changing the existing array or item directly.

Recap

  • Treat React state objects and arrays as read-only.
  • Mutation keeps the same reference, which can make state updates invisible to equality checks.
  • Create new objects and arrays with spread, map(), and filter().
  • For nested updates, copy each object along the path you change; spread is shallow.

What's next?

Next, we’ll learn what the Fetch API is and how to use it to request data.

Written by

Dev Likhon

Dev Likhon

Full Stack Developer & Tech Educator

6+ years of professional development experience.

About Me
Share:

Comments

Share what you learned or ask a question.

Loading comments...