首页 > 解决方案 > TypeError:无法读取未定义的属性“startDate”

问题描述

我的反应组件中有以下错误

TypeError: Cannot read property 'startDate' of undefined

我知道这是因为我没有绑定我的函数。当我hour在我的localStorage. 但是现在当我custom在我的localStorage. 而且它也可以在 componentWillMount 中使用,它具有hourcustom. 所以这是我的代码

class Home extends Component {
  constructor (props) {
    super(props)
    this.state = {
      filterType: this.getFilterType(localStorage.getItem('daysFilter') ? localStorage.getItem('daysFilter') : 'today'),
    }
  }
    getFilterType (type) {
    let filterType = 'hour'
    if (type === 'today') {
      filterType = 'hour'
    }
    if (type === "custom" ){
      const startDate = this.state.startDate
      const endDate   = this.state.endDate
    }
    return filterType
  }
}

但我需要知道如何在构造函数中绑定我的函数?

标签: javascriptreactjs

解决方案


当您实例化时this.state,您正在调用尚未初始化的getFilterType(type)哪些使用。this.state


推荐阅读