componentWillReceiveProps 周期函數調用 this.state.start 發現總是慢一步
父組件引入了三個子組件。當父組件的日期改變時,更改 state 里面的 start_time end_time, 此時子組件需要接收父組件傳過來的日期值,並重新調用接口獲取新數據。
如,現在是設備報警,當父組件日期更改時,會觸發子組件 componentWillReceiveProps 這個生命周期函數;而切換到發動機報警的時候,觸發的是engine組件的 componentDidMount 這個生命周期函數。
父組件傳過來的值,在componentWillReceiveProps(nextProps) 周期函數中可以接收並設置 setState({start_time: nextProps.start}),但是,在后面的函數中不要直接調用 this.state.start_time . ----查看函數生命周期,此時設置了setState,但是需要在等幾個生命周期函數后才能被設置。-----因而在componentWillReceiveProps 調用 this.state.start_time.會發現,這個日期總比現在傳過來的參數晚一次。 在componentWillReceiveProps 中函數就調用nextProps.start這個參數就可以。