瞭解React組件的生命週期react
React流程狀態圖ajax
注意:流程狀態圖爲使用React.createClass方法的生命週期segmentfault
ajax
獲取後臺數據渲染時,通常將調用ajax
方法放在componentDidMount
中,這樣能夠先渲染虛擬dom
再展現數據,當再次調用ajax
數據改變時,dom
內數據會再次渲染,而不用再次加載整個dom
。若是在該dom要根據條件只經過ajax
獲取一次數據,則能夠將調用ajax
的方法放在componentWillMount
。jQuery
,異步請求也可使用fetch
等componentWillUpdate
中,儘可能避免使用setState()
或setProps()
方法。若無條件判斷狀況下使用setState()
或setProps()
,會致使死循環,一樣componentDidUpdate
中使用setState()
若無條件限制也會致使死循環。shouldComponentUpdate
能夠對是否進行渲染的條件判斷,可以做爲性能調優的手段,避免無心義渲染。componentWillReceiveProps
能夠經過nextProps
獲取新傳入的參數值,例如:nextProps.operationType
獲取operationTypesetState()
的週期爲:componentWillMount
、componentDidMount
、componentWillReceiveProps
、componentDidUpdate
this.state
的值經過構建列表樹後總結補充:dom
setState
不會當即生效,要通過render
過程才能真正改變state
值return
時調用方法setState
,會引發shouldComponentUpdate
週期,而此時componentDidMount
週期已完成。React組件生命週期過程說明
React組件生命週期
React數據獲取爲何必定要在componentDidMount裏面調用?異步