摘要:組件生命周期構(gòu)造方法是對(duì)類(lèi)的默認(rèn)方法,通過(guò)命令生成對(duì)象實(shí)例時(shí)自動(dòng)調(diào)用該方法。該生命周期可以發(fā)起異步請(qǐng)求,并。后廢棄該生命周期,可以在中完成設(shè)置渲染組件是一個(gè)組件必須定義的生命周期,用來(lái)渲染。該生命周期內(nèi)可以進(jìn)行。
React組件生命周期 constructor( ) 構(gòu)造方法
constructor是ES6對(duì)類(lèi)的默認(rèn)方法,通過(guò) new 命令生成對(duì)象實(shí)例時(shí)自動(dòng)調(diào)用該方法。并且,該方法是類(lèi)中必須有的,如果沒(méi)有顯示定義,則會(huì)默認(rèn)添加空的constructor( )方法。當(dāng)存在constructor的時(shí)候??必須手動(dòng)調(diào)用super方法。
在constructor中如果要訪問(wèn)this.props需要傳入props,示例如下:
class MyClass extends React.component{ constructor(props){ super(props); // 聲明constructor時(shí)必須調(diào)用super方法 console.log(this.props); // 可以正常訪問(wèn)this.props } }
constructor 常用來(lái)初始化state
class MyClass extends React.Component { constructor(props){ super(props); this.state = { list: this.props.List }; } }
Class靜態(tài)方法實(shí)例屬性 初始化state
class ReactCounter extends React.Component { state = { list: [] }; }
具體文章可見(jiàn)Class-的靜態(tài)方法
componentWillMount() 組件掛載之前在組件掛載之前調(diào)用且全局只調(diào)用一次。如果在這個(gè)鉤子里可以setState,render后可以看到更新后的state,不會(huì)觸發(fā)重復(fù)渲染。該生命周期可以發(fā)起異步請(qǐng)求,并setState。(React v16.3后廢棄該生命周期,可以在constructor中完成設(shè)置state)
render() ?渲染組件render是一個(gè)React組件必須定義的生命周期,用來(lái)渲染dom。??不要在render里面修改state,會(huì)觸發(fā)死循環(huán)導(dǎo)致棧溢出。render必須返回reactDom
render() { const {nodeResultData: {res} = {}} = this.props; if (isEmpty(res)) return noDataInfo; const nodeResult = this.getNodeResult(res); return (componentDidMount() 組件掛載完成后{nodeResult});
在組件掛載完成后調(diào)用,且全局只調(diào)用一次??梢栽谶@里使用refs,獲取真實(shí)dom元素。該鉤子內(nèi)也可以發(fā)起異步請(qǐng)求,并在異步請(qǐng)求中可以進(jìn)行setState。
componentDidMount() { axios.get("/auth/getTemplate").then(res => { const {TemplateList = []} = res; this.setState({TemplateList}); }); }componentWillReceiveProps (nextProps ) props即將變化之前
props發(fā)生變化以及父組件重新渲染時(shí)都會(huì)觸發(fā)該生命周期,在該鉤子內(nèi)可以通過(guò)參數(shù)nextProps獲取變化后的props參數(shù),通過(guò)this.props訪問(wèn)之前的props。該生命周期內(nèi)可以進(jìn)行setState。(React v16.3后廢棄該生命周期,可以用新的周期 static getDerivedStateFromProps 代替)
shouldComponentUpdate(nextProps, nextState) 是否重新渲染組件掛載之后,每次調(diào)用setState后都會(huì)調(diào)用shouldComponentUpdate判斷是否需要重新渲染組件。默認(rèn)返回true,需要重新render。返回false則不觸發(fā)渲染。在比較復(fù)雜的應(yīng)用里,有一些數(shù)據(jù)的改變并不影響界面展示,可以在這里做判斷,優(yōu)化渲染效率。
componentWillUpdate(nextProps, nextState)shouldComponentUpdate返回true或者調(diào)用forceUpdate之后,componentWillUpdate會(huì)被調(diào)用。不能在該鉤子中setState,會(huì)觸發(fā)重復(fù)循環(huán)。(React v16.3后廢棄該生命周期,可以用新的周期 getSnapshotBeforeUpdate )
componentDidUpdate() 完成組件渲染除了首次render之后調(diào)用componentDidMount,其它render結(jié)束之后都是調(diào)用componentDidUpdate。該鉤子內(nèi)setState有可能會(huì)觸發(fā)重復(fù)渲染,需要自行判斷,否則會(huì)進(jìn)入死循環(huán)。
componentDidUpdate() { if(condition) { this.setState({..}) // 設(shè)置state } else { // 不再設(shè)置state } }componentWillUnmount() 組件即將被卸載
組件被卸載的時(shí)候調(diào)用。一般在componentDidMount里面注冊(cè)的事件需要在這里刪除。
生命周期圖 完整的生命周期示例class LifeCycle extends React.Component { constructor(props) { super(props); this.state = {str: "hello"}; } componentWillMount() { alert("componentWillMount"); } componentDidMount() { alert("componentDidMount"); } componentWillReceiveProps(nextProps) { alert("componentWillReceiveProps"); } shouldComponentUpdate() { alert("shouldComponentUpdate"); return true; // 記得要返回true } componentWillUpdate() { alert("componentWillUpdate"); } componentDidUpdate() { alert("componentDidUpdate"); } componentWillUnmount() { alert("componentWillUnmount"); } render() { alert("render"); return(React v16.3 新加入的生命周期 (轉(zhuǎn)載) react v16.3刪掉以下三個(gè)生命周期); } }{parseInt(this.props.num)}
{this.state.str}
componentWillMount
componentWillReceiveProps
componentWillUpdate
新增兩個(gè)生命周期static getDerivedStateFromProps
getSnapshotBeforeUpdate
static getDerivedStateFromProps觸發(fā)時(shí)間:在組件構(gòu)建之后(虛擬dom之后,實(shí)際dom掛載之前) ,以及每次獲取新的props之后。
每次接收新的props之后都會(huì)返回一個(gè)對(duì)象作為新的state,返回null則說(shuō)明不需要更新state.
配合componentDidUpdate,可以覆蓋componentWillReceiveProps的所有用法
class Example extends React.Component { static getDerivedStateFromProps(nextProps, prevState) { // 沒(méi)錯(cuò),這是一個(gè)static } }getSnapshotBeforeUpdate
觸發(fā)時(shí)間: update發(fā)生的時(shí)候,在render之后,在組件dom渲染之前。
返回一個(gè)值,作為componentDidUpdate的第三個(gè)參數(shù)。
配合componentDidUpdate, 可以覆蓋componentWillUpdate的所有用法。
class Example extends React.Component { getSnapshotBeforeUpdate(prevProps, prevState) { // ... } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/95253.html
摘要:完整生命周期初始化參數(shù)第一次渲染當(dāng)父組件向子組件傳入發(fā)生改變后,依次調(diào)用子組件更新渲染當(dāng)組件自身發(fā)生變化后組件再次更新渲染當(dāng)組件卸載生命周期詳解此處請(qǐng)求接口數(shù)據(jù)子組件獲得新時(shí)觸發(fā),作用是在子組件再次渲染前,更新子組件自身的,之后會(huì)觸發(fā)接受的 完整生命周期 constructor(props) // 初始化參數(shù) componentWillMount() render() // 第一次...
摘要:這個(gè)階段只有一個(gè)方法,該方法在整個(gè)生命周期內(nèi)調(diào)用且僅調(diào)用一次。在這里進(jìn)行一些相關(guān)的銷(xiāo)毀操作,比如撤銷(xiāo)定時(shí)器,事件監(jiān)聽(tīng)等等。 詳解 React 生命周期 整個(gè) React 生命周期有3個(gè)階段:創(chuàng)建、更新、卸載,每個(gè)階段有對(duì)應(yīng)的工作和方法,我們可以看下面這個(gè)經(jīng)典的圖研究一下: showImg(https://segmentfault.com/img/remote/1460000016330...
摘要:本文主要介紹之后的生命周期。該方法有兩個(gè)參數(shù)和返回值為對(duì)象不需要返回整體,把需要改變的返回即可。必須有一個(gè)返回值,返回的數(shù)據(jù)類(lèi)型可以有。此生命周期主要用于優(yōu)化性能。最后,說(shuō)明一點(diǎn)這三個(gè)生命周期在未來(lái)版本中會(huì)被廢棄。 React16.3.0開(kāi)始,生命周期進(jìn)行了一些變化。本文主要介紹React16.3.0之后的生命周期。 React16.3.0之前生命周期: 16版本之前的react組件的...
摘要:除此之外指的是繞過(guò)通過(guò)直接添加的事件處理函數(shù)和產(chǎn)生的異步調(diào)用。但是,當(dāng)在調(diào)用事件處理函數(shù)之前就會(huì)調(diào)用,這個(gè)函數(shù)會(huì)把修改為,造成的后果就是由控制的事件處理過(guò)程不會(huì)同步更新。 拋出問(wèn)題 class Example extends Component { contructor () { super() this.state = { value: 0, ...
閱讀 1467·2021-11-24 09:39
閱讀 3641·2021-09-29 09:47
閱讀 1584·2021-09-29 09:34
閱讀 3087·2021-09-10 10:51
閱讀 2552·2019-08-30 15:54
閱讀 3230·2019-08-30 15:54
閱讀 883·2019-08-30 11:07
閱讀 1022·2019-08-29 18:36