裝載過程:
Constructor:
初始化state严蓖;
綁定成員函數(shù)的this環(huán)境(bind、:: )防止以解構的方式進行全局作用域進行調(diào)用挡毅;
例:this.fn=this.fn.bind(this)或this.fn=::this.fn
GetInitialState:初始化state值,必須結合React.createClass()使用迈喉,使用es6的方式叮雳,此函數(shù)無效果;
getInitialState:function(){
return {
name:"zhangsan"
}
},
getDefaultProps:function(){
return {
name:"zhangsan" //這里的zhangsan相當于默認值
}
}
})
GetDefaultProps:初始化props值,必須結合React.createClass()使用麻敌,使用es6的方式栅炒,此函數(shù)無效果,用屬性defaultProps代替术羔;
constructor(props){
super(props)
this.state={data:new Date()}
}
render(){
return (
<ul>
<li><ClickCount name="third" num={7}></ClickCount></li>
</ul>
)
}
}
Test.defaultProps={
name:"moren"
}
ComponentWillMount:在render函數(shù)調(diào)用之前進行調(diào)用赢赊;
Render:并不做實際的渲染動作,只返回一個jsx描述結構级历,最后渲染與否由react來決定释移;必須是個純函數(shù),不能設計到數(shù)據(jù)的更變(this.setState)寥殖;
return (
<ul>
<li><ClickCount name="third" num={7}></ClickCount></li>
</ul>
)
}
ComponentDidMount :在render函數(shù)調(diào)用之后進行調(diào)用玩讳;但不是在render函數(shù)調(diào)用之后立即調(diào)用涩蜘,而是當所有的dom樹掛載并渲染完成后才會調(diào)用,這是因為render函數(shù)并不進行渲染熏纯,而只是返回一個jsx對象同诫,渲染的工作由react庫進行,只有當所有的組件的jsx對象組合完畢之后豆巨,react通過對比后才會渲染剩辟,所有此鉤子函數(shù)是發(fā)生的所有組件的render函數(shù)都執(zhí)行后才會執(zhí)行;(只能在瀏覽器端觸發(fā))
注:與angular中的link或者post函數(shù)有點相似往扔,但是這里不僅指子組件贩猎,而是當前組件中的所有組件,包括兄弟組件萍膛;提供了dom操作的接口
更新過程:
ComponentWillReceiveProps: (參數(shù)只有props)傳入的props的改變或者組件進行刷新(forceUpdate函數(shù)觸發(fā)組件的刷新)都會觸發(fā)此函數(shù)吭服,但是通過this.setState改變的數(shù)據(jù)則不會觸發(fā)此函數(shù),
ShouldComponentUpdate:react組件的鉤子函數(shù)兩個需要有返回值的鉤子函數(shù)之一蝗罗,另一個為render艇棕,此鉤子函數(shù)的返回值為一個bolen值,如果為true時串塑,則prop的改變以及state的改變都會引起組件的刷新沼琉,如果為false時,則不再進行渲染桩匪;此鉤子函數(shù)接受兩個參數(shù)打瘪,一個是nextProps,一個是nextState傻昙,可以將將要更新的值和此時的做對比闺骚,然后返回true和false來進行性能的校優(yōu);
ComponentWillUpdate:跟componentWillMount相似
Render
ComponentDidUpdate:跟componentDidMount相似
卸載過程:
ComponentWillUnmount:此鉤子函數(shù)可以在組件卸載前執(zhí)行妆档,可以進行手動添加dom元素的刪除僻爽,以及計時器和事件監(jiān)聽的移除;
使用方法:
setNumber(){
this.setState({
count:this.state.count+1
},()=>{
this.componentWillUnmount() //調(diào)用銷毀組件
})
}
//銷毀生命周期
componentWillUnmount(){
if(this.state.count>3){
//將要銷毀的組件寫入 括號中
ReactDOM.unmountComponentAtNode(document.getElementById('root'))
}
}
點擊button按鈕 (實現(xiàn)數(shù)字加一贾惦,如果大于3時數(shù)字銷毀組件)
<button onClick={this.props.setNumber}>{this.props.count}</button>
1:construct 類似 Vue 中的 beforeCreated 和 Created胸梆;
操作:(1)數(shù)據(jù)的請求(2)當前組件的檢測;
2:componentWillMount
介紹:類似Vue 中 beforemount 纤虽;在render函數(shù)調(diào)用之前進行調(diào) 用乳绕;:
3:render 純函數(shù) ;
操作 (1)不能調(diào)用setstate進行數(shù)據(jù)的改變逼纸,否則會陷入死循環(huán)
(2)不做數(shù)據(jù)的產(chǎn)生和保存洋措,
(3)返回數(shù)據(jù)和Dom拼接好的jsx結構
注:(1)因為數(shù)據(jù)更新后會觸發(fā)render周期 ,所以render中進行數(shù)據(jù)改變會陷入死循環(huán)(2)必須有返回值return
4:ComponentDidMount
介紹:dom編譯完成,并渲染到真實dom中時調(diào)用
操作:(1)可以做dom的處理
(2)可以做具體的事件監(jiān)聽
(3)某些插件的實例化
5:componentWillReceiveProps
介紹:當父組件給子組件的數(shù)據(jù)改變時觸發(fā)該函數(shù)
操作 (1)可以將props再次賦給state,也可以說是用來監(jiān)聽props改變
6:shouldComponentUpdate(props,state)
介紹:對于項目提高渲染性能時使用杰刽;
操作 (1)可以對props和state的值進行對比 然后通過返回true和false 來控制是否更新組件菠发;(在瀏覽器->more tools->rendering里勾選Paing flashing可以高亮頁面重繪的部分)
注:(1)必須有返回值 ture || false
7:componentWillUpdate
介紹 :在render之前最后一次可以改變數(shù)據(jù)的地方
8:componentDidUpdate 同 componentWillMount
9:ComponentWillUnmount:
介紹 :此鉤子函數(shù)可以在組件卸載前執(zhí)行王滤,可以進行手動添加dom元素的 刪除,以及計時器和事件監(jiān)聽的移除滓鸠;
操作:(1)所有跟dom相關的操作雁乡,在組建銷毀之前一定要在這里做相應的處理;否則會出錯
整個react的組件的生命周期可以分成幾種情況去執(zhí)行:
組件第一次渲染:
getDefaultProps -> getInitialState -> componentWillMount -> render -> componentDidMount
卸載組件:
componentWillUnmount
組件第二次渲染:
getInitialState -> componentWillMount -> render -> componentDidMount
組件props發(fā)生改變:
componentWillReceiveProps -> shouldComponentUpdate -> componentWillUpdate -> render -> componentDidUpdate
組件state發(fā)生改變(props并未改變):
shouldComponentUpdate -> componentWillUpdate -> render -> componentDidUpdate