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

315
Views
Why changing an object value with useState in React does not work using variable.property syntax?

I'm new to React and I'm wondering why the following code does not works

const Component = () => {

  var [person, setPerson] = useState({
    name: 'bob',
    age: 21,
    message: 'hello!'
  })

const changeName = () => { 

    person.name = 'brian'
    console.log(person) // {name: 'brian', age: 21, message: 'hello!'}
    setPerson(person)

  }

return <>
    <h5>{person.name}</h5>
    <button onClick={changeName}>change the name</button>
  </>

However, if I do it by other ways, like the following or using spread operator, it works:

const changeName = () => { 

    person = { 
      name: 'brian'
    }

    setPerson(person)
    
  }
about 4 years ago · Santiago Trujillo
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!