Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!