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

133
Views
UseState bug in React

I'm starting with React and I need to use the 'useState()' function in a site.

First, I'm creating the useState variable: const [messages, setMessages] = useState(['1111', '222'])

Then I'm returning a div with the messages.

Up to here it's working, but I have to use my 'SetMessages' command:

setMessages(...messages, '333')

And when I write it , my site it stops working and all I see is a white page. I've seen many videos about it and I literraly copied pasted their codes them and it still didn't work, what's wrong with my code and how to use this second command from 'useState' right?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

When using the spread operator (...) you still need to to wrap that in an object or an array. So [...x, y] or {...x, y}.

In your case

setMessages([...messages, '333'])
about 4 years ago · Juan Pablo Isaza Report

0

Use this


setMessages(prevMsgs=>[...prevMsgs,"333"])

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!