当我绑定(bind)变量时,我觉得做了不必要的例行工作
this.handleInputChange = this.handleInputChange.bind(this);
或声明 that = this
以便在异步处理程序中使用当前对象。
有没有办法在原型(prototype)中处理这个例程?
最佳答案
我对 React 很陌生,但是,我知道使用箭头函数可以简化这项工作:
import React from 'react';
class Input extends React.Component {
handleInputChange = () => {
// do something
}
render() {
return(
<div>
<input onChange={this.handleInputChange} />
</div>
)
}
这样你就不需要在构造函数中绑定(bind)方法。
在此讨论中查看更多内容:Can you bind arrow functions?
关于javascript - 如何停止绑定(bind)每个处理程序并声明变量 'that',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51565480/