React Easy technical 0 views 2 min read

How do you update objects inside state?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of React conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

You cannot update the objects which exists in the state directly. Instead, you should create a fresh new object (or copy from the existing object) and update the latest state using the newly created object. Eventhough JavaScript objects are mutable, you need to treat objects inside state as read-only while updating the state.

Let's see this comparison with an example. The issue with regular object mutation approach can be described by updating the user details fields of Profile component. The properties of Profile component such as firstName, lastName and age details mutated in an event handler as shown below.

      import { useState } from "react";

      export default function Profile() {
        const [user, setUser] = useState({
          firstName: "John",
          lastName: "Abraham",
          age: 30,
        });

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

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

        function handleAgeChange(e) {
          user.age = e.target.value;
        }

        return (
          <>
            <label>
              First name:
              <input value={user.firstName} onChange={handleFirstNameChange} />
            </label>
            <label>
              Last name:
              <input value={user.lastName} onChange={handleLastNameChange} />
            </label>
            <label>
              Age:
              <input value={user.age} onChange={handleAgeChange} />
            </label>
            <p>
              Profile:
              {person.firstName} {person.lastName} ({person.age})
            </p>
          </>
        );
      }
      

Once you run the application with above user profile component, you can observe that user profile details won't be update upon entering the input fields.
This issue can be fixed by creating a new copy of object which includes existing properties through spread syntax(...obj) and add changed values in a single event handler itself as shown below.

      handleProfileChange(e) {
        setUser({
        ...user,
          [e.target.name]: e.target.value
        });
      }
      

The above event handler is concise instead of maintaining separate event handler for each field. Now, UI displays the updated field values as expected without an issue.

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?