javascript - 为什么报错 'searchInput' of undefined

标签 javascript reactjs redux

我想找人。数据数组存储在 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/

相关文章:

javascript - render 似乎在 componentDidMount 之前触发(使用 async/await)

javascript - react-router-dom 不工作只是渲染 "/"

reactjs - 从 Redux 状态中提取属性返回未定义,尽管它肯定有一个值

javascript - 为什么纯 reducer 在 redux 中如此重要?

reactjs - react 和 Redux : Uncaught Error: A state mutation was detected between dispatches

javascript - jvectormap 标记标签图像

javascript - webpack 4 react 意外 token ...(扩展运算符)

javascript - foreach 中的绑定(bind) knockout 下拉列表

javascript - 将 props 从 react-cosmos 传递到 styled-components

javascript - TypeScript:TypeError:App 不是构造函数