2017-02-14 6 views
1

ich einen Fehler wie folgt festgestellt:undefiniert ist kein Objekt (Bewertung '_this.props.date.getFullYear')

enter image description here

und das ist mein Code

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

Ich mag Holen Sie sich eine Standard-Daten von this.props.date, aber ich weiß nicht, warum ich den Fehler

+1

Bitte zeigen Wie geht es Ihnen 'vorbei date' –

+0

prop wie sind vorbei Sie Datum als Stütze auf die Starview Komponente –

+0

die console.log setzen (Requisiten) in Konstruktor und fügen Sie den Ausgang in ur ques. –

Antwort

4

Sie haben definiert propTypes, aber nicht defaultProps. Wie ich verstehe, was Sie wollen, ist ein Standardwert für die Prop date. In diesem Fall müssen defaultProps definiert werden. Hier ein Beispiel:

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