Skip to content
Justice Tech School

State with Objects

Never mutate state. Copy it with spread and change a field.

Example
const [user, setUser] = useState({ name: "Ada", age: 30 });
function birthday() {
  setUser({ ...user, age: user.age + 1 });
}

Exercise

Copy then change.

setUser({ user, age: 31 })

Quiz Test

Why not mutate state directly?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.