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

79
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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