我试图解决 React 烦人的bind
要求如下:
class ExtendedComponent extends React.Component {
custom_functions: [];
constructor(props){
super(props);
let self = this;
for (let i = 0; i < this.custom_functions.length; i++) {
let funcname = this.custom_functions[i];
self[funcname] = self[funcname].bind(self);
}
}
}
class OrderMetricsController extends ExtendedComponent {
custom_functions: ['refreshTableOnDateChange', 'load', 'refreshTableOnTabChange'];
constructor(props){
super(props);
...
这将排除需要
this.refreshTableOnDateChange = this.refreshTableOnDateChange.bind(this);
现在,我知道TypeError: Cannot read property 'length' of undefined
问题出在哪里this.custom_functions.length
。