question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Managing child component state

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
gcazaciuccommented, Sep 2, 2016

Implemented in 1.3 as discussed above. Docs updated( check the recipes section)

0reactions
gcazaciuccommented, Sep 3, 2016

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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found