gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'startDate' of undefined

转载 作者:行者123 更新时间:2023-12-02 23:28:01 40 4
gpt4 key购买 nike

我的 react 组件中出现以下错误

TypeError: Cannot read property 'startDate' of undefined

我知道这是因为我没有绑定(bind)我的函数。当我的 localStorage 中有 hour 时,它就可以工作。但现在当我的 localStorage 中有 custom 时。而且它也可以在 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
}
}

但是我需要知道如何在构造函数内绑定(bind)我的函数?

最佳答案

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

关于javascript - 类型错误 : Cannot read property 'startDate' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56643049/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com