我试图解决作出反应的恼人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
。
JavaScript没有“属性”。当你说“班级属性”时,你的意思是什么? –
'custom_functions:[...];'肯定看起来像'class'里面的语法错误。你使用的是什么风格的JavaScript? – Bergi
如果在ES6类语法中使用'bind'使你恼火,你是否考虑过_not_使用ES6类语法,并恢复使用'.createClass'和'createElement'等React帮助函数。 – Pineda