Created a base React component to inherit from:
import { Component } from 'react';
class BaseComponent extends Component {
constructor(props) {
super(props);
}
_bindProps(...methods) {
return methods.map(method => this[method].bind(this))
}
}
export default BaseComponent;
And my child component:
class SomeChild extends BaseComponent {
constructor(props) {
super(props);
}
foo() {
}
render() {
const props = this._bindProps('foo');
<Child {...props} />
}
}
However, I receive Cannot read property 'bind' of undefined
on the line return methods.map(method => this[method].bind(this))
. How can I achieve this, ie. passing down methods from a parent component to a child, and when it's called from the child, have it's this
value reference the parent component.