2017-02-14 1 views
1

я обнаружил ошибку вроде этого:неопределенный не является объект (оценки '_this.props.date.getFullYear')

enter image description here

и это мой код

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

Я хочу получить данные по умолчанию из этого.props.date, но я не знаю, почему я получаю сообщение об ошибке

+1

Пожалуйста, покажите Как ты проходя 'date' проп –

+0

как вы проходя дату в качестве опоры для компонента StarView –

+0

положить console.log (реквизита) в конструкторе и добавить вывод в ур вопрос. –

ответ

4

Вы определили , но не defaultProps. Как я понимаю, то, что вы хотите, является значением по умолчанию для prop date. В этом случае defaultProps - это то, что вам нужно определить. Вот пример:

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

 Смежные вопросы

  • Нет связанных вопросов^_^