一、組件執(zhí)行的生命周期: 參考 https://www.cnblogs.com/soyxiaobi/p/9559117.html 或 https://www.cnblogs.com/kdcg/p/9182393.html(含生命周期函數(shù) 傳進(jìn)來(lái)的參數(shù))
1、初始沒(méi)有改變state、props 的生命周期:
constructor、componentWillMount蜓谋、render 担忧、【子組件對(duì)應(yīng)這4個(gè)周期函數(shù)】馁启、componentDidMount 依次執(zhí)行
2岗喉、改變 state 后的生命周期:
a秋度、父組件的 state 改變:
shouldComponentUpdate、componentWillUpdate沈堡、render静陈、【子組件的 componentWillReceiveProps、子組件對(duì)應(yīng)父組件這4個(gè)周期函數(shù)】诞丽、componentDidUpdate
父組件的的state改變鲸拥,會(huì)引起子組件 state 相關(guān)的生命周期函數(shù)運(yùn)行。
b僧免、子組件的 state 改變:
shouldComponentUpdate刑赶、componentWillUpdate负蚊、render苹威、componentDidUpdate
子組件的state改變,不會(huì)引起父組件的變化不从。
3浊洞、改變 props 后的 生命周期:【props改變牵敷,不會(huì)引起父子組件的任何變化,state變化才引起子組件的變化】
父組件傳遞給子組件的props改變法希,不會(huì)引起任何變化枷餐。只有父組件state改變,父組件render函數(shù)運(yùn)行苫亦,所有子組件遞歸更新毛肋。
所以父組件傳遞給子組件的props值,一般使用state的值屋剑,不然給子組件的props值改變了润匙,但是沒(méi)有辦法傳遞到子組件中,得等觸發(fā)了父組件的render函數(shù)唉匾,才能把數(shù)據(jù)傳遞給子組件孕讳。
父組件的 state 設(shè)置,都會(huì)觸發(fā)子組件的 componentWillReceiveProps 生命周期函數(shù)巍膘,且把函數(shù)參數(shù)是props值卫病。