Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - How do I pass a state two tiers up, and then down to another child?

Tags:

reactjs

state

I have a state from <Left Child 2/> that I wish to pass to <Right Child 2/>. They both have one parent(the one above), and refer to the same parent container.

The structure is something like this:

     <ParentContainer />
        |            |
        |            |
        v            v
<Left Child 1/> <Right Child 1/>
        v            v
<Left Child 2/> <Right Child 2/>

How do I pass data not one, but two tiers up in React, and then propagate it down another tree from the same Parent?

like image 336
cbll Avatar asked Sep 16 '25 21:09

cbll


1 Answers

Add a function and default internal state in your parent component. Then pass down the function into <Left Child 2/> and call the function there which will set the passed data as a state of the parent component, which you can pass down to <Right Child 1/> with props.

The key here is to have a single source of truth for your data, which should be the parent component.

like image 78
Shota Avatar answered Sep 18 '25 15:09

Shota