Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
React set input value to object property not editable

I'm setting the value of an input to a property on an object (from the state). I have an onChange event to update the state. However, when I try to type, it doesn't actually update in the input. If I don't use an object and just have a string, it works fine. How can I fix this?

Demo of the issue
My code:

import React from "react";

export function App() {
  const [data, setData] = React.useState({
    test: "hello"
  });
  
  const change = (e) => {
    data.test = e.target.value;
    setData(data);
  };

  return <input value={data.test} onChange={change}></input>;
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Per the comment by Aman Sadhwani, I used spread syntax to update the state without modifying the original object.

const change = (e) => {
    setData({
        ...data,
        test: e.target.value
    });
};
about 4 years ago · Juan Pablo Isaza Report

0

Thanks, @Aman Sadhwani. Use Spread operator to update the state.

import React from "react";

export function App() {
  const [data, setData] = React.useState({
    test: "hello"
  });
  
  const change = (e) => {
    setData({
    ...data
    test:e.target.value
    });
  };

  return <input value={data.test} onChange={change}></input>;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!