$1.42

How to pass state between siblings in React

Ask a question+
1

hi i am new to react redux i am working on a project and in that project i have a checkbox menu when user select anything from that menu then the state is update noew i want to pass that state into another component how can i do that
here is my code for checkbox menu (this is my dropdown class)

<MultiSelect
      options={this.state.zone_account}
      selected={this.state.selected}
      onSelectedChanged={this.selected}
    />
    selected = (selected) => {
    // selectedFruits is array of { label, value }
    this.setState({ selected },()=>{
        console.log("selected list checkbox",this.state.selected)
        // sessionStorage.setItem(recent_slected_user_accountsid+"selected zones",this.state.selected)
        sessionStorage.setItem(recent_slected_user_accountsid+"selected zones_ID",this.state.selected)

})
}

here is selected state that i want to pass into another component and the code of another component is
this i smy anohter compoent
```
class Overview extends Component {
constructor(props) {
super(props);
}
render(){
return(
<div>
//here i want to use that slelected state
</div>)
}

add comment

1 Answer

1

You can do simply by doing this:
Parent.js (Parent component)

import Overview from './Overview'; //Import your overview component

<MultiSelect
      options={this.state.zone_account}
      selected={this.state.selected}
      onSelectedChanged={this.selected}
    />
    selected = (selected) => {
    // selectedFruits is array of { label, value }
    this.setState({ selected },()=>{
        console.log("selected list checkbox",this.state.selected)
        // sessionStorage.setItem(recent_slected_user_accountsid+"selected zones",this.state.selected)
        sessionStorage.setItem(recent_slected_user_accountsid+"selected zones_ID",this.state.selected)
})
}
<Overview selectedValue={this.state.selected}/> //<-Note this

Basically where you are selecting state in that same component you need to import Overview component.And then you can easily pass state from there to another components

add comment

Your Answer