class Parent extends React.Component {
constructor(props) {
super(props)
this.handleChildOnClick = this.handleChildOnClick.bind(this)
}
handleChildOnClick() {
/* I would like to do something here that requires both Child and Parent.
* But 'this' points to Parent because I binded it in Parent's constructor.
*/
}
render() {
return(
<Child
onClick={this.handleChildOnClick}
/>)
}
}
In the handleChildOnClick
callback, I would like to call a Child's method to retrieve some data and update Parent's state accordingly.
If I bind Parent's this
to handleChildOnClick
in Parents constructor, then I lose reference to Child, thus unable to call Child's method. But if I don't bind it, then I'm unable to set Parent's state inside handleChildOnClick
.
Is there a way to bind this
while not overriding the original this
?
Edit:
My Child component is from a library so I can't modify it.
Edit:
I'm using react-google-maps library. The child component is GoogleMap, and I would like to store the new view bounds of GoogleMap in Parent's state (by calling getBounds()
on GoogleMap) whenever the onBoundsChange
callback of GoogleMap gets fired.