javascript - 未捕获的类型错误 : Cannot read property 'props' of undefined in react-redux

标签 javascript reactjs react-redux redux-thunk

当我尝试更改react-redux中的状态属性时,出现错误Uncaught TypeError:无法读取未定义的属性“props”,这是我要更改的代码:

class Home extends Component {
  componentWillMount(){
    this.props.fetchMatches();
  }

  handleChange(newDateRange){
    this.props.fetchMatches({
      ...this.props,
      startDate: newDateRange[0],
      endDate: newDateRange[1]
    })
  }

最佳答案

执行以下操作:

handleChange = (newDateRange) => {
    this.props.fetchMatches({
      ...this.props,
      startDate: newDateRange[0],
      endDate: newDateRange[1]
    })
  }

或者在构造函数中执行

constructor(){
    super(props);
    this.handleChange = this.handleChange.bind(this);
}

handleChange中,无法找到context,因此this未定义。您必须显式绑定(bind) this 或使用 arrow function

关于javascript - 未捕获的类型错误 : Cannot read property 'props' of undefined in react-redux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51233566/

相关文章:

javascript - AngularJS渲染更新记录+Laravel后端

javascript - AngularJS:TypeError:undefined 不是带有链式选择的函数

javascript - 如何修复 babel react "Uncaught SyntaxError: Unexpected token <"

javascript - 使用react-datepicker仅选择并更新选定div中的日期

javascript - 通过 React.js 将 vis.js 网络渲染到容器中

javascript - react : How to redirect

javascript - 解析 React 中字符串内的嵌套 HTML?

javascript - 如何禁用所有键盘按键?

reactjs - 单元测试 - 使用 Jest 和 Enzyme 在 React 中链接

javascript - 如何在 react redux 中添加 protected 路由