上流程圖描述了組件從建立、運行到銷燬的整個過程,能夠看到若是一個組件在被建立,從開始一直到運行會依次調用getDefaultProps
到render
這五個函數;在運行過程當中,若是有屬性和狀態的改變,又會觸發左側的其餘函數的調用,並在此回到運行狀態;當組件即將會被銷燬時,會調用函數conponentWillUnmount
來通知組件,到最終組件銷燬,生命週期結束。安全
getDefaultProps 獲取默認屬性,並初始化props;
getInitialState 獲取初始化的組件狀態state;
componentWillMount 組件將會被裝載,在渲染render前調用;
componentWillReceiveProps 若是接收到屬性就會調用該方法,舊的屬性仍然能夠經過this.props來獲取,也能夠調用this.setState來更新組件的狀態,這裏更新狀態是安全的,不會觸發render。
shouldComponentUpdate 決定是否更新組件;
componentWillUpdate 若是組件的狀態或者屬性改變了,而且shouldComponentUpdate爲true,就會調用側方法準備更新組件;
render渲染,即初次渲染和更新組件的方法;
componentDidUpdate 組件更新完成後會調用此方法;
conponentWillUnmount 當組件要銷燬,即從界面移除時,就會調用此方法。
在ES6中已經廢除了getDefaultProps
和getInitialState
的方式,直接經過this.props
和this.state
來獲取。函數