我想找人。数据数组存储在 redux 存储中。我正在尝试执行搜索,但出现错误。
无法读取未定义的属性“searchInput”
可能是什么问题?
searchHandler(){
console.log('findUser', this.searchInput.value);
this.props.onFindPeople(this.searchInput.value)
}
我添加一个例子code
最佳答案
您将 this
的上下文丢失给了事件处理程序。
您可以使用 arrow function (为 this
使用词法上下文):
searchHandler = () => {
console.log("findUser", this.searchInput.value);
this.props.onFindPeople(this.searchInput.value);
}
或bind它像这样上课:
constructor(props){
super(props);
this.searchHandler = this.searchHandler.bind(this);
}
我们在 constructor
中执行它,因为它只会运行一次。
关于javascript - 为什么报错 'searchInput' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52581258/