Updating Objects in State

State can hold any kind of JavaScript value, including objects. But you shouldn't change objects that you hold in the React state directly. Instead, when you want to update an object, you need to create a new one (or make a copy of an existing one), and then set the state to use that copy.

  • How to correctly update an object in React state
  • How to update a nested object without mutating it
  • What immutability is, and how not to break it
  • How to make object copying less repetitive with Immer

What's a mutation?

You can store any kind of JavaScript value in state.

const [x, setX] = useState(0);

So far you've been working with numbers, strings, and booleans. These kinds of JavaScript values are "immutable", meaning unchangeable or "read-only". You can trigger a re-render to replace a value:

setX(5);

The x state changed from 0 to 5, but the number 0 itself did not change. It's not possible to make any changes to the built-in primitive values like numbers, strings, and booleans in JavaScript.

Now consider an object in state:

const [position, setPosition] = useState({ x: 0, y: 0 });

Technically, it is possible to change the contents of the object itself. This is called a mutation:

position.x = 5;

However, although objects in React state are technically mutable, you should treat them as if they were immutable--like numbers, booleans, and strings. Instead of mutating them, you should always replace them.

Treat state as read-only

In other words, you should treat any JavaScript object that you put into state as read-only.

This example holds an object in state to represent the current pointer position. The red dot is supposed to move when you touch or move the cursor over the preview area. But the dot stays in the initial position:

```js {expectedErrors: {'react-compiler': [11]}} import { useState } from 'react';

export default function MovingDot() { const [position, setPosition] = useState({ x: 0, y: 0 }); return (

{ position.x = e.clientX; position.y = e.clientY; }} style={{ position: 'relative', width: '100vw', height: '100vh', }}>
translate(${position.x}px, ${position.y}px), left: -10, top: -10, width: 20, height: 20, }} />
); }


```css
body { margin: 0; padding: 0; height: 250px; }

The problem is with this bit of code.

onPointerMove={e => {
  position.x = e.clientX;
  position.y = e.clientY;
}}

This code modifies the object assigned to position from the previous render. But without using the state setting function, React has no idea that object has changed. So React does not do anything in response. It's like trying to change the order after you've already eaten the meal. While mutating state can work in some cases, we don't recommend it. You should treat the state value you have access to in a render as read-only.

To actually trigger a re-render in this case, create a new object and pass it to the state setting function:

onPointerMove={e => {
  setPosition({
    x: e.clientX,
    y: e.clientY
  });
}}

With setPosition, you're telling React:

  • Replace position with this new object
  • And render this component again

Notice how the red dot now follows your pointer when you touch or hover over the preview area:

import { useState } from 'react';

export default function MovingDot() {
  const [position, setPosition] = useState({
    x: 0,
    y: 0
  });
  return (
    <div
      onPointerMove={e => {
        setPosition({
          x: e.clientX,
          y: e.clientY
        });
      }}
      style={{
        position: 'relative',
        width: '100vw',
        height: '100vh',
      }}>
      <div style={{
        position: 'absolute',
        backgroundColor: 'red',
        borderRadius: '50%',
        transform: `translate(${position.x}px, ${position.y}px)`,
        left: -10,
        top: -10,
        width: 20,
        height: 20,
      }} />
    </div>
  );
}
body { margin: 0; padding: 0; height: 250px; }

[!INFO]

Local mutation is fine

Code like this is a problem because it modifies an existing object in state:

position.x = e.clientX;
position.y = e.clientY;

But code like this is absolutely fine because you're mutating a fresh object you have just created:

const nextPosition = {};
nextPosition.x = e.clientX;
nextPosition.y = e.clientY;
setPosition(nextPosition);

In fact, it is completely equivalent to writing this:

setPosition({
  x: e.clientX,
  y: e.clientY
});

Mutation is only a problem when you change existing objects that are already in state. Mutating an object you've just created is okay because no other code references it yet. Changing it isn't going to accidentally impact something that depends on it. This is called a "local mutation". You can even do local mutation while rendering. Very convenient and completely okay!

Copying objects with the spread syntax

In the previous example, the position object is always created fresh from the current cursor position. But often, you will want to include existing data as a part of the new object you're creating. For example, you may want to update only one field in a form, but keep the previous values for all other fields.

These input fields don't work because the onChange handlers mutate the state:

```js {expectedErrors: {'react-compiler': [11, 15, 19]}} import { useState } from 'react';

export default function Form() { const [person, setPerson] = useState({ firstName: 'Barbara', lastName: 'Hepworth', email: 'bhepworth@sculpture.com' });

function handleFirstNameChange(e) { person.firstName = e.target.value; }

function handleLastNameChange(e) { person.lastName = e.target.value; }

function handleEmailChange(e) { person.email = e.target.value; }

return ( <>

{person.firstName}{' '} {person.lastName}{' '} ({person.email})

</> ); }


```css
label { display: block; }
input { margin-left: 5px; margin-bottom: 5px; }

For example, this line mutates the state from a past render:

person.firstName = e.target.value;

The reliable way to get the behavior you're looking for is to create a new object and pass it to setPerson. But here, you want to also copy the existing data into it because only one of the fields has changed:

setPerson({
  firstName: e.target.value, // New first name from the input
  lastName: person.lastName,
  email: person.email
});

You can use the ... object spread syntax so that you don't need to copy every property separately.

setPerson({
  ...person, // Copy the old fields
  firstName: e.target.value // But override this one
});

Now the form works!

Notice how you didn't declare a separate state variable for each input field. For large forms, keeping all data grouped in an object is very convenient--as long as you update it correctly!

import { useState } from 'react';

export default function Form() {
  const [person, setPerson] = useState({
    firstName: 'Barbara',
    lastName: 'Hepworth',
    email: 'bhepworth@sculpture.com'
  });

function handleFirstNameChange(e) {
    setPerson({
      ...person,
      firstName: e.target.value
    });
  }

function handleLastNameChange(e) {
    setPerson({
      ...person,
      lastName: e.target.value
    });
  }

function handleEmailChange(e) {
    setPerson({
      ...person,
      email: e.target.value
    });
  }

return (
    <>
      <label>
        First name:
        <input
          value={person.firstName}
          onChange={handleFirstNameChange}
        />
      </label>
      <label>
        Last name:
        <input
          value={person.lastName}
          onChange={handleLastNameChange}
        />
      </label>
      <label>
        Email:
        <input
          value={person.email}
          onChange={handleEmailChange}
        />
      </label>
      <p>
        {person.firstName}{' '}
        {person.lastName}{' '}
        ({person.email})
      </p>
    </>
  );
}
label { display: block; }
input { margin-left: 5px; margin-bottom: 5px; }

Note that the ... spread syntax is "shallow"--it only copies things one level deep. This makes it fast, but it also means that if you want to update a nested property, you'll have to use it more than once.

[!INFO]

Using a single event handler for multiple fields

You can also use the [ and ] braces inside your object definition to specify a property with a dynamic name. Here is the same example, but with a single event handler instead of three different ones:

import { useState } from 'react';

export default function Form() {
  const [person, setPerson] = useState({
    firstName: 'Barbara',
    lastName: 'Hepworth',
    email: 'bhepworth@sculpture.com'
  });

function handleChange(e) {
    setPerson({
      ...person,
      [e.target.name]: e.target.value
    });
  }

return (
    <>
      <label>
        First name:
        <input
          name="firstName"
          value={person.firstName}
          onChange={handleChange}
        />
      </label>
      <label>
        Last name:
        <input
          name="lastName"
          value={person.lastName}
          onChange={handleChange}
        />
      </label>
      <label>
        Email:
        <input
          name="email"
          value={person.email}
          onChange={handleChange}
        />
      </label>
      <p>
        {person.firstName}{' '}
        {person.lastName}{' '}
        ({person.email})
      </p>
    </>
  );
}
label { display: block; }
input { margin-left: 5px; margin-bottom: 5px; }

Here, e.target.name refers to the name property given to the <input> DOM element.

Updating a nested object

Consider a nested object structure like this:

const [person, setPerson] = useState({
  name: 'Niki de Saint Phalle',
  artwork: {
    title: 'Blue Nana',
    city: 'Hamburg',
    image: 'https://react.dev/images/docs/scientists/Sd1AgUOm.jpg',
  }
});

If you wanted to update person.artwork.city, it's clear how to do it with mutation:

person.artwork.city = 'New Delhi';

But in React, you treat state as immutable! In order to change city, you would first need to produce the new artwork object (pre-populated with data from the previous one), and then produce the new person object which points at the new artwork:

const nextArtwork = { ...person.artwork, city: 'New Delhi' };
const nextPerson = { ...person, artwork: nextArtwork };
setPerson(nextPerson);

Or, written as a single function call:

setPerson({
  ...person, // Copy other fields
  artwork: { // but replace the artwork
    ...person.artwork, // with the same one
    city: 'New Delhi' // but in New Delhi!
  }
});

This gets a bit wordy, but it works fine for many cases:

```js import { useState } from 'react';

export default function Form() { const [person, setPerson] = useState({ name: 'Niki de Saint Phalle', artwork: { title: 'Blue Nana', city: 'Hamburg', image: 'https://react.dev/images/docs/scientists/Sd1AgUOm.jpg', } });

function handleNameChange(e) { setPerson({ ...person, name: e.target.value }); }

function handleTitleChange(e) { setPerson({ ...person, artwork: { ...person.artwork, title: e.target.value } }); }

function handleCityChange(e) { setPerson({ ...person, artwork: { ...person.artwork, city: e.target.value } }); }