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

151
Views
Spread Operator when setting State in React

I have the following object in default state:

const [data, setData] = useState({name: "", products: []})

I have 2 Select elements with an onChange event handler:

<Select onChange={() => setData({...data, products: [...data.products, {id: 1, ...data.products[0]}]})})</Select>

<Select onChange={() => setData({...data, products: [...data.products, {...data.products[0], qty: 1}]})})</Select>

When I do a

console.log(data);

I get {name: "", products[{qty: 1},...]} only. How do I make this so the id is appended to the array as well?

about 4 years ago · Juan Pablo Isaza
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!