You can also use the es6 arrow function in your parent component, so there is no need to explicitly associate this with this function.
However, when we call this function from the child with the id that should be passed to it, we bind this to pass the id value. Below is a modified code for reference. Hope this helps.
class Parent extends React.Component {
// make this function as arrow function
suggestionClick = (id) => {
console.log(this.props, id); // {props Object} , id
}
render(){
return (
<ChildComponent click={this.suggestionClick} />
);
}
}
const ChildComponent = (props) => (
<SubChildComponent id="1" click={props.click} />
);
const SubChildComponent = (props) => (
<div className="subsubcomponent" onClick={props.click.bind(this, props.id)} />
);
ReactDOM.render(
<Parent />,
document.getElementById('app')
);
source
share