Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

Use this


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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda