Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

Use this


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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda