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

121
Vistas
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 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