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

140
Visualizações
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 à 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