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

78
Views
How can I recursively update object properties in hook?

I have the below function in order to recursively display IP subnets by user click:

const traverse = (o: any) => {
  return o.map((item: any, key: number) => {
    let extra = item.hasOwnProperty("extra") ? item.extra : item;
    return (
      <TreeItem
        key={key}
        nodeId={extra.id}
        icon={item.has_child ? <ChevronLeftIcon /> : ""}
        label={item.subnet}
        onClick={
          extra.has_child ? (e) => loadChildren(extra, extra.id) : () => ""
        }
      >
        {item.child ? traverse(item.child) : ""}
      </TreeItem>
    );
  });
};

Now I have put the fetch action in loadChildren where I would set the newly fetched data:

const loadChildren = (extra: any, id: number | string) => {
  setLoadingChildren(true);
  getIpState(id)
    .then((res) => {
      setRows({
        ...rows,
        data: rows.data.map((item: any) =>
          item.id == id ? { ...item, child: res.data } : item
        ),
      });
      setLoadingChildren(false);
    })
    .catch((err) => {
      console.log(err);
      setLoadingChildren(false);
    });
};

Now the question is, how can I iterate through child: res.data and add their own child: res.data having the response of newly fetched data?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Finally it was solved like this:

  const findClickedItem = (items: any,res: any,id:any) => {
  return items.map((item: any) => {
          let extra = item.hasOwnProperty('extra') ? item.extra : item
          return extra.id == id ? {
              ...item,
              child: res.data
          } : item.hasOwnProperty('child')
              ? {...item, child: findClickedItem(item.child, res, id)} : item
      }
  ) 
}

And is used in loadChildren like this:

  const loadChildren = (id: number | string) => {
      setLoadingChildren(true)
      getIpState(id).then(res=> {
          setRows({
              ...rows,
              data: findClickedItem(rows.data, res, id)
          })
          setLoadingChildren(false)
      }).catch(err => {
          console.log(err)
          setLoadingChildren(false)
      })
  }
about 4 years ago · Juan Pablo Isaza Report
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!