I am trying to pass a function down to a child then down another level to that child's child then have that pass it's id back up to the parent but I can't figure out how to pass the id back up. I can get the function to get called without passing parameters back but I can't get it to work with it.
Here is my parent component:
class SideBar extends Component {
constructor(props){
super(props);
this.state={
groups: [
{name: 'Test', id: 1},
],
open: false,
selectedIndex: 1
};
}
handleSelectedIndex = (id) => {
console.log('selected id: ' + id);
};
render(){
return(
<div className={classes.Groups}>
<Groups groups={this.state.groups} selected={this.state.selectedIndex} handleSelected={this.handleSelectedIndex}/>
</div>
);
}
Here is the first child:
const groups = (props) => (
<List component="nav" className={classes.GroupContainer}>
{
props.groups.map(group =>
<Group
name={group.name}
key={group.id}
id={group.id}
selected={props.selected}
handleSelected={props.handleSelected}/>
)
}
</List>
);
And here is the child's child:
const group = (props) => {
let id = props.id
return(
<>
<ListItem
button
selected={props.selected === props.id}
className={classes.Group}
onClick={props.handleSelected(id)}
>
<ListItemText
className={classes.GroupTitle}
primary={props.name}
key={props.key}/>
</ListItem>
<Divider className={classes.Divider}/>
</>
);
};
Can someone please explain how to pass the id back up?