Javascript TypeError:无法读取属性';起始日期';未定义的
我的react组件中有以下错误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
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,明白了。那么解决办法是什么呢?