Skip to main content

Command Palette

Search for a command to run...

React | Updating Objects in React with useState

Published
1 min readView as Markdown
V

Front-end developer with a background in Korean language QA and education.
Experienced in localisation testing, cross-functional teamwork, and user-focused development.
I enjoy crafting intuitive web experiences with React, Next.js, and TypeScript — always learning, always building.

Starting Point

Here's a basic useState setup:

Initial values

const [person, setPerson] = useState({
  name: "peter",
  age: 24,
  hobby: "read books",
});

The Mistake If you try to update only one property like this:

setPerson({ name: "susan" });

You will lose the other values (age and hobby). Now person only has a name.

Solution

Use the spread operator to keep the existing values:

setPerson({ ...person, name: "susan" });

This copies the old person object and updates the name.

Complete Example

import { useState } from "react";

const UseStateObject = () => {
  const [person, setPerson] = useState({
    name: "peter",
    age: 24,
    hobby: "read books",
  });

  const displayPerson = () => {
    setPerson({ ...person, name: "susan" });
  };

  return (
    <>
      <h3>{person.name}</h3>
      <h3>{person.age}</h3>
      <h3>Enjoys: {person.hobby}</h3>
      <button className="btn" onClick={displayPerson}>
        show susan
      </button>
    </>
  );
};

export default UseStateObject;

Why It Matters

React needs to see a new object to know that something changed. That’s why we don’t just change person.name directly. Instead, we create a new object with the updated values.

Using the spread operator like this keeps your state updates safe and clear.

More from this blog

Valencia Log.dev

9 posts

Hello, I’m Valencia — a self-taught frontend developer based in the UK. This blog is where I share what I’m learning, building, and occasionally messing up. Simple notes, honest progress.