【发布时间】:2015-11-30 22:26:56
【问题描述】:
创建了一个基础的 React 组件来继承:
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;
还有我的子组件:
class SomeChild extends BaseComponent {
constructor(props) {
super(props);
}
foo() {
}
render() {
const props = this._bindProps('foo');
<Child {...props} />
}
}
但是,我在return methods.map(method => this[method].bind(this)) 线上收到了Cannot read property 'bind' of undefined。我怎样才能做到这一点,即。将方法从父组件传递给子组件,当从子组件调用时,让其 this 值引用父组件。
【问题讨论】:
标签: reactjs bind ecmascript-6