undefined 不是一个对象(评估'_this.props.date.getFullYear') [英] undefined is not an object (evaluating '_this.props.date.getFullYear')
本文介绍了undefined 不是一个对象(评估'_this.props.date.getFullYear')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了这样的错误:
这是我的代码
export default class StarView extends Component{
static propTypes = {
date : React.PropTypes.instanceOf(Date)
}
constructor(props){
super(props);
this.state = {
selectedYear: this.props.date.getFullYear(),
selectedMonth: this.props.date.getMonth(),
selectedDate: this.props.date.getDate(),
yesterdayYear : new Date(this.props.date.getTime() - 24 * 3600 * 1000).getFullYear(),
yesterdayMonth: new Date(this.props.date.getTime() - 24 * 3600 * 1000).getMonth(),
yesterdatDate : new Date(this.props.date.getTime() - 24 * 3600 * 1000).getDate(),
tomorrowYear : new Date(this.props.date.getTime() + 24 * 3600 *1000).getFullYear(),
tomorrowMonth : new Date(this.props.date.getTime() + 24 * 3600 *1000).getMonth(),
tomorrowDate : new Date(this.props.date.getTime() + 24 * 3600 *1000).getDate()
}
}}
我想从 this.props.date 获取默认数据,但我不知道我收到错误的原因
I want get a default data from this.props.date ,but I don't know the reason I get the error
推荐答案
您已经定义了 propTypes 但不是 defaultProps.据我了解,您想要的是道具 date
的默认值.在这种情况下,defaultProps 是您需要定义的.下面是一个例子:
You have defined propTypes but not defaultProps. As I understand, what you want is a default value for the prop date
. In that case defaultProps is what you need to define. Here is an example:
export default class StarView extends Component{
static propTypes = {
date: React.PropTypes.instanceOf(Date)
}
static defaultProps = {
date: new Date()
}
constructor(props){
super(props);
this.state = {
selectedYear: this.props.date.getFullYear(),
selectedMonth: this.props.date.getMonth(),
selectedDate: this.props.date.getDate(),
yesterdayYear: new Date(this.props.date.getTime() - 24 * 3600 * 1000).getFullYear(),
yesterdayMonth: new Date(this.props.date.getTime() - 24 * 3600 * 1000).getMonth(),
yesterdatDate: new Date(this.props.date.getTime() - 24 * 3600 * 1000).getDate(),
tomorrowYear: new Date(this.props.date.getTime() + 24 * 3600 *1000).getFullYear(),
tomorrowMonth: new Date(this.props.date.getTime() + 24 * 3600 *1000).getMonth(),
tomorrowDate: new Date(this.props.date.getTime() + 24 * 3600 *1000).getDate()
}
}
}
这篇关于undefined 不是一个对象(评估'_this.props.date.getFullYear')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文