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

139
Vistas
Cloning large data structure ReactJS makes value go missing

Hullo there, I have an array which each value has a deep Tree object. I'm keeping a version of this in a useState hook and another in a useRef. My idea is to make the changes on the ref and therefore update the state. My problems started on keeping the array ordered, so I added an order key to sort it.

This is a wrapper function to update the state, it isn't sorting nor cloning (either shallow or deep) the array makes the last value disappear

  const updateScope = (newScopes = null as ScopesProps[]) => {
    if (newScopes) {
      scopesRef.current = newScopes;
    }

    const supposedToBeSorted = scopesRef.current.sort(
      (a, b) => a.order - b.order
    );

    console.log("aha, gotcha", supposedToBeSorted, [...supposedToBeSorted]);

    setScopes([...supposedToBeSorted]);
  };

And this is what I got logged

aha, gotcha 
[{…}]
0: {id: 110134, main: {…}, loaded: false, reorderCriteria: '', order: 1}
1: {id: 107950, main: {…}, loaded: false, reorderCriteria: '', order: 0}
length: 2
[[Prototype]]: Array(0)
 
[{…}]
0: {id: 110134, main: {…}, loaded: false, reorderCriteria: '', order: 1}
length: 1
[[Prototype]]: Array(0)
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