Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在ReactJS中的导出函数中使用this.state_Javascript_Reactjs_Export_State_Bind - Fatal编程技术网

Javascript 如何在ReactJS中的导出函数中使用this.state

Javascript 如何在ReactJS中的导出函数中使用this.state,javascript,reactjs,export,state,bind,Javascript,Reactjs,Export,State,Bind,我试图使用App组件中导出的函数中的this.state。 绑定方法不起作用。有什么想法吗 我试过: 内部error-handler.js 内部App.js 尝试在App.js内运行errorHandler()时的结果: "Cannot read property 'bind' of undefined" 谢谢你的帮助 this.errorHandler = errorHandler.bind(this); 因为您刚刚导出了函数,它还不是类的一部分,errorHandler只是一个导出的函数

我试图使用App组件中导出的函数中的
this.state
。 绑定方法不起作用。有什么想法吗

我试过:

内部error-handler.js

内部App.js

尝试在App.js内运行errorHandler()时的结果:

"Cannot read property 'bind' of undefined"
谢谢你的帮助

this.errorHandler = errorHandler.bind(this);

因为您刚刚导出了函数,它还不是类的一部分,
errorHandler
只是一个导出的函数。因此,您必须像下面这样绑定方法

constructor(props) {
    super(props);
    this.errorHandler = errorHandler.bind(this); // bind event
}
...
...

<input 
 {...props}
 onClick={e => this.errorHandler()} // call method
/>
构造函数(道具){
超级(道具);
this.errorHandler=errorHandler.bind(this);//绑定事件
}
...
...
this.errorHandler()}//调用方法
/>

我编辑了这一行,删除了赋值运算符后面的“this”关键字,但仍然不起作用。有什么想法吗?没有任何想法,因为我不明白你说的“仍然不工作”是什么意思……这行出现了异常,我指出了原因,现在你可以把它称为.errorHandler(),它应该可以工作了
this.errorHandler = errorHandler.bind(this);
constructor(props) {
    super(props);
    this.errorHandler = errorHandler.bind(this); // bind event
}
...
...

<input 
 {...props}
 onClick={e => this.errorHandler()} // call method
/>