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

120
Visualizações
React only adds to state when button is clicked again

bit of an odd issue, I have an action that updates the state, but this state is only updated when the button is clicked a second time.

   function addComponent(component: Component) {
        setSelectedComponents(oldArray => [...oldArray, component])

        console.log(selectedComponents)
    }

this is called when clicking on a button

    {components.map((component) => (
                                <div
                                    key={component.name}
                                    onClick={() => addComponent(component)}
                                    className="collapse w-96 border rounded-box border-base-300 collapse-plus my-2">
                                </div>
                            ))}

console.logging the result shows that, on the first click it's still an empty array and clicking the button again shows the object inside of the array.

any idea what's going on here? this is driving me crazy.

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