Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取属性';起始日期';未定义的_Javascript_Reactjs - Fatal编程技术网

Javascript TypeError:无法读取属性';起始日期';未定义的

Javascript TypeError:无法读取属性';起始日期';未定义的,javascript,reactjs,Javascript,Reactjs,我的react组件中有以下错误 TypeError: Cannot read property 'startDate' of undefined 我知道这是因为我没有绑定我的函数。它在我的localStorage中有hour时工作。但是现在当我的localStorage中有custom时。而且它也可以在组件willmount中工作,它有hour或custom。这是我的代码 class Home extends Component { constructor (props) { su

我的react组件中有以下错误

TypeError: Cannot read property 'startDate' of undefined
我知道这是因为我没有绑定我的函数。它在我的
localStorage
中有
hour
时工作。但是现在当我的
localStorage
中有
custom
时。而且它也可以在组件willmount中工作,它有
hour
custom
。这是我的代码

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
  }
}

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

当您实例化
this.state
您正在调用
getFilterType(type)
使用的是尚未初始化的
状态。

这是因为
状态
尚未初始化。您的状态初始时没有这些值,您在哪里设置
状态
?@SagarJajoriya知道了。那么解决办法是什么呢?@Harish Ok,明白了。那么解决办法是什么呢?