javascript - TypeError:无法读取未定义的属性“startDate”
问题描述
我的反应组件中有以下错误
TypeError: Cannot read property 'startDate' of undefined
我知道这是因为我没有绑定我的函数。当我hour
在我的localStorage
. 但是现在当我custom
在我的localStorage
. 而且它也可以在 componentWillMount 中使用,它具有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)
哪些使用。this.state
推荐阅读
- javascript - cy.get() 如何在 Cypress 中放置 2 个参数?
- excel - 选择索引集中的第二高行
- ruby - 为什么 Hash.new 中的默认数组需要键值规范?
- android - 底部导航栏的导航组件不起作用
- javascript - 正则表达式匹配数字+字符串
- javascript - 改变antd上传文件的位置
- java - libGDX 错误失败:构建失败并出现异常
- php - Laravel 8 - 在线支付 przelewy24 网关实现
- c# - 从 asp.net 核心流式传输大型数据集时性能不佳
- quarkus - Quarkus:AWS Secrets Manager 的自定义凭证提供程序