Update object state immutably
Replace state objects instead of editing them
An object in state can group related values, such as a delivery address and delivery option. Treat the stored object as read-only: construct a replacement and request an update with the setter. The following independent App.jsx examples use the Chapter 3 project and automatic JSX transformation. Visible outcomes are predictions pending browser tests.
import { useState } from 'react';
export default function App() { const [delivery, setDelivery] = useState({ city: 'Oslo', express: false }); return <main><h1>Delivery options</h1> <p>{delivery.city}: {delivery.express ? 'Express' : 'Standard'}</p> <button type="button" onClick={() => setDelivery(d => ({ ...d, express: !d.express }))}> Switch delivery option </button> </main>;}Initially the page says Oslo: Standard. One activation requests a new object with express set to true; the city remains Oslo. Object spread copies own enumerable properties into a new object. Later properties override earlier ones, so the changed field belongs after the spread. Unlike a class component state method, the useState setter replaces the whole value rather than merging its fields.
Copy each changed level
import { useState } from 'react';
function changeCity(order, city) { return { ...order, address: { ...order.address, city } };}
export default function App() { const [order, setOrder] = useState({ address: { city: 'Oslo', country: 'Norway' }, items: [{ id: 'mug', quantity: 2 }] }); return <main><h1>Shipping address</h1> <p>{order.address.city}, {order.address.country}</p> <button type="button" onClick={() => setOrder(o => changeCity(o, 'Bergen'))}>Ship to Bergen</button> </main>;}After activation the city is Bergen. The original order and address remain unchanged; the items array can be shared because it was not edited. A shallow copy only copies references to nested objects, not those objects themselves. Copy from the changed field back up to the state root. Deeply nested state may be easier to manage with flatter structures; this lesson keeps a small nested object to show the rule.
Recognize a shallow-copy bug
import { useState } from 'react';
export default function App() { const [order, setOrder] = useState({ address: { city: 'Oslo' } }); function change() { const next = { ...order }; next.address.city = 'Bergen'; setOrder(next); } return <main><h1>Shipping address</h1><p>{order.address.city}</p> <button type="button" onClick={change}>Change city</button> </main>;}This may appear to work because the outer reference changes, but next.address is the same object as order.address. The earlier snapshot has been modified. Calling a setter with the original object itself also risks React skipping the update because its identity is unchanged. Use the nested-copy pattern above. Mutation of a fresh, unshared object is different from mutation of existing state.
Practice
Add a postalCode field to the nested address and update only that field. Predict which references change and which remain shared. Check that the old address still contains its initial postal code.