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

156
Vistas
Complex array state not setting in parent component and useeffect not updating

I have come across some strange behaviour in a component I am making.

I have some inputs that I map out and a onchange which passes the value to the parent component. I then add this data to an array depending on what data is sent.

I have setArray which is my state but it doesn't appear to get set?

    const [array, setArray]= useState([])

    function onChange(e, __typename, valid, value){
    console.log(e, __typename, valid, value) // this returns mobile 01882828 true 0 if i am in the mobile input
        const newSelectedArray = array;
        if(array.includes(__typename)){
            newSelectedArray.push({
                ...value,
                __typename: __typename,
                data: e,
                isValid: valid
              });
            setArray(newSelectedArray)
        }else{
            array[value] = {
                ...value,
                __typename: __typename,
                data: e, 
                isValid: valid
            }
            setArray(array)
        }
    //console.log(array) this works and logs my array
    }


    useEffect(() => {
        console.log(array); //this does not log my array
      }, [array])

My return

...
<OnboardingContent sections={sections} sendToRegister={onChange}/>
<button onClick={() => console.log(array)}>Log</button> //This logs the array as well
<button onClick={() => setArray(array)}>State</button> //This does nothing
...

example console.log output

[
    {
        "__typename": "mobile",
        "data": "01882828",
        "isValid": true
    },
    {
        "__typename": "firstname",
        "data": "Hello",
        "isValid": false
    }
]

If someone could tell me why none of this is working

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