第三單元(react組件的生命周期)
課程目標(biāo)
靈活掌握react組件的生命周期以及組件的活動(dòng)過程。
能夠靈活使用react的生命周期
知識點(diǎn)
- react的類組件的生命周期分為三個(gè)階段
- 實(shí)例期
- 存在期
- 銷毀期
- 實(shí)例期在組件第一次被實(shí)例化的時(shí)候觸發(fā)一次,在這個(gè)過程中會(huì)執(zhí)行的生命周期函數(shù)如下:
- constructor
- componentWillMount
- render
- componentDidMount
- 存在期分為兩種情況:
- 在組件內(nèi)部調(diào)用了
this.setState,此時(shí)會(huì)觸發(fā)的生命周期如下:- shouldComponentUpdate
- componentWillUpdate
- render
- componentDidUpdate
- 該組件的屬性被再次傳入的時(shí)候,此時(shí)會(huì)觸發(fā)的生命周期如下:
- componetWillReceiveProps
- shouleComponentUpdate
- componentWillUpdate
- render
- componentDidUpdate
-
銷毀期指的是組件被卸載的時(shí)候,此時(shí)有一個(gè)聲明周期函數(shù)會(huì)執(zhí)行:(一般這個(gè)生命周期函數(shù)中可以做一些清除的工作)
- compoentWillUnmount
一般在
constructor componentWillMount componentDidMount這些生命周期中初請求接口。盡量不要在始化調(diào)用componentWillUpdate componentDidUpdate render中去調(diào)用請求接口,也不要去寫太多的邏輯、不要調(diào)用this.setState。每個(gè)生命周期接收的參數(shù)
- componentWillReceiveProps(nextProps){}
- shouldComponentUpdate(nextProps, nextState){}
- componentWillUpdate(nextProps, nextState){}
- componentDidUpdate(prevProps, prevState){}
-
react生命周期圖示
tao-react生命周期圖示.png
授課思路

第三單元(react組件的生命周期).png
