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

128
Views
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 answers
Answer question

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 Report

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 Report

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