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

130
Vistas
How to props through the deeply nested child to parent data ? React

To explain the situation. I have a child component that has one chevron that spins on a click. I need to implement from child to parent and to grand parent props data.

<ChildComponent>
<ParentComponent>
<GrandParentComponent>

Inside ChildComponent I have arrow icon ( img )

  const [rotateIcon, setRotateIcon] = useState(false);

  const expandAttachment = useCallback(() => {
    setRotateIcon(!rotateIcon); 
  }, [rotateIcon]);


  <img src="assets/arrow" transform={rotateIcon ? 'rotate(90deg)' : 'rotate(0)'} >

And this is work but.... I need to props rotateIcon state to GrandParentComponent compoentent.

It’s not just the child to the parent. Than child to parent and from parent another level up to. GrandParent.

How do I do that? Is this a good way at all? What are my alternatives? I do not use redux in the system, but api context!

To be clear. All three components are connected. Each to each is a child parent!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can do prop drilling i.e, adding a state to <GrandParentComponent/> component and passing it as prop to the child components.

grandparent

export default GrandParentComponent(){
    const [rotateIcon, setRotateIcon] = useState(false);

    // pass the state and setState down to the children
    return <ParentComponent rotateIcon={rotateIcon} setRotateIcon={setRotateIcon}/> 
}

parent

export default ParentComponent({rotateIcon, setRotateIcon}){
    // pass props to child component again
    return <ChildComponent rotateIcon={rotateIcon} setRotateIcon={setRotateIcon}/> 
}

child

export default ChildComponent({rotateIcon, setRotateIcon}){
  const expandAttachment = useCallback(() => {
    setRotateIcon(!rotateIcon); 
  }, [rotateIcon]);

  <img src="assets/arrow" transform={rotateIcon ? 'rotate(90deg)' : 'rotate(0)'} >
}

There’s nothing inherently bad about prop drilling, but it gets harder to manage the state of the app as the complexity increases.

To avoid this we either use React Context or Redux. React Context is baked into react itself, so it's very lightweight and powerful at the same time. It is the perfect fit for a situation like yours.

To keep the answer short, read on how to implement React Context and useContext hook here.

about 4 years ago · Juan Pablo Isaza Denunciar

0

store the state in the GrandParentComponent and pass through in between components as a prop to reach ChildComponent. Props are meant to be passed from a parent component to a child component(s).

about 4 years ago · Juan Pablo Isaza Denunciar

0

As I see it you have 3 options:

  1. Store the state props in the parent of all components, which is too complex for this purpose I think.
  2. Use Redux to handle state management for the app
  3. Use Context API to handle the state management for the app as a global state. You can check out this blog for this: https://www.loginradius.com/blog/engineering/react-context-api/#:~:text=What%20is%20Context%20API%3F,to%20parent%2C%20and%20so%20on.

I do however use and recommend using redux for complex operations like this. When you understand the idea behind redux it becomes very easy to use it.

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