Managing child component state
See original GitHub issueI’m trying your library for a new project, and I just hit a small problem.
Let’s consider the following structure:
<SplitView id="my-splitview">
<SplitViewPane>
something
</SplitViewPane>
<SplitViewPane>
something else
</SplitViewPane>
</SplitView>
I’d like the SplitView
component to manage the state of it’s children, and I’d like the children to be able to read props and dispatch actions in the context of the SplitView
.
For example, this is what I have to do now:
function render(props) {
let items = []
props.children.forEach((child, i) => {
items.push(React.cloneElement(child, {
key: i,
horizontal: props.horizontal,
collapsed: props.panes[i].collapsed,
height: props.panes[i].height,
width: props.panes[i].width,
toggleCollapsed: props.toggleCollapsed
}))
})
return (
<div>
{ items }
</div>
)
}
export default
local({
key: (props) => props.id,
createStore: (props) => {
let panes = props.children.map((child) => child.props)
return createStore(reduce, { panes })
},
mapStateToProps: (state, props) => {
return {
panes: state.panes
}
},
mapDispatchToProps: (dispatch) =>({
toggleCollapsed: (index) => dispatch(toggleCollapsed(index))
})
})(render)
In this example, the toggleCollapsed
action is passed to the child, along with the state.
What I’d rather do, is to only pass an index
propery to my children panes, and have a local
in my children looking like this:
// in child
function render(props) {
return (
<div className="this-is-split-view-pane" onClick={props.toggleCollapsed}>
{ props.children }
</div>
)
}
export default
local({
key: // parent key, this is inherited somehow
createStore: // this is inherited somehow
mapStateToProps: (state, props) => {
return state.panes[props.index] // here I can access the PARENT state
},
mapDispatchToProps: (dispatch) =>({
// this dispatch to the parent store
toggleCollapsed: (index) => dispatch(toggleCollapsed(index))
})
})(render)
This code might have typos, I tried to simplify my real case into an understandable example.
But I hope the paradigm is simple enough.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:11 (11 by maintainers)
Top Results From Across the Web
Passing State of Parent to Child Component as Props
First, click on App and observe its state under the Hooks section on the right pane. Second, click on a given player component...
Read more >A Simple Way to Get Child State in the Parent Component ...
1- Elevating the state: The most common solution is to manage the state of the “CHILD” in the “PARENT” component, so in “PARENT”...
Read more >Sharing State Between Components - React Docs
Sometimes, you want the state of two components to always change together. To do it, remove state from both of them, move it...
Read more >React js change child component's state from parent component
The state should be managed in the parent component. You can transfer the open value to the child component by adding a property....
Read more >How to change the state of a child component from its parent ...
First, we will create a Superhero component with a name attribute in state. This component will render that name first.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Implemented in 1.3 as discussed above. Docs updated( check the recipes section)
I will also add some unit tests in the coming days to prove this use case works just fine and ensure it stays that way as we continue making changes/fixing bugs.