React生命周期詳解

Each component has several "lifecycle methods" that you can override to run code at particular times in the process. Methods prefixed with will are called right before something happens, and methods prefixed with did are called right after something happens. --- React官方文檔

掛載

初始化過程如下:

  • constructor
  • componentWillMount
  • render
  • componentDidMount

更新

當(dāng)父組件給子組件傳值時,會觸發(fā)如下更新過程:

  • componentWillReceiveProps
  • shouldComponentUpdate
  • componentWillUpdate
  • render
  • componentDidUpdate

卸載

當(dāng)一個組件被從DOM中移除時,該方法別調(diào)用:

  • componentWillUnmount

具體加載過程參考如下圖:


具體代碼

  1. 掛載
        class CycleLife extends React.Component {
            // 掛載過程
            // 1.構(gòu)造器函數(shù)
            constructor(props) {
                super(props);
                console.log('構(gòu)造器函數(shù)');
            }
            // 2.組件將要掛載
            componentWillMount() {
                console.log('組件將要被掛載');
            }
            // 3.組件渲染
            render() {
                console.log('組件被渲染');
                return (
                    <h1>Hello, World</h1>
                );
            }
            // 4.組件已經(jīng)掛載
            componentDidMount() {
                console.log('組件已經(jīng)被掛載');
            }
        }
    
  2. state 變更觸發(fā)更新
        class CycleLife extends React.Component {
            constructor(props) {
                super(props)
                this.state = {
                    time: new Date()
                }
            }
            changeTime = () => {
                this.setState({
                    time: new Date()
                })
            }
            componentDidMount() {
                // 每隔一秒修改時間, 實現(xiàn)時鐘效果
                setInterval(this.changeTime, 1000)
            }
            // 1. 判斷是否需要組件更新, 默認(rèn) true
            shouldComponentUpdate(nextProps, nextState) {
                // 返回 false 時, 后續(xù)函數(shù)不執(zhí)行
                return true
            }
            // 2. 組件將要更新
            componentWillUpdate(nextProps, nextState) {
                console.log('nextProps: ', nextProps)
                console.log('nextState: ', nextState)
                console.log('組件將要被掛載')
            }
            // 3. 組件被重新渲染
            render() {
                console.log('組件被重新渲染')
                return (
                    <h1>{this.state.time.toLocaleString()}</h1>
                );
            }
            // 4. 組件已經(jīng)更新
            componentDidUpdate() {
                console.log('組件已經(jīng)被掛載')
            }
        }
        ReactDOM.render(
            <CycleLife/>,
            document.getElementById('root')
        );
    
  3. 父 -> 子 傳遞屬性時, 發(fā)生的生命周期
        class CycleLife extends React.Component {
            static propTypes = {
                content: React.PropTypes.string
            }
            constructor(props) {
                super(props)
            }
            // 1. 父 -> 子傳遞props時, 觸發(fā)
            componentWillReceiveProps(nextProps, nextState) {
                console.log('組件屬性被父級修改')
                console.log('nextProps: ', nextProps)
                console.log('nextState: ', nextState)
            }
            // 2. 判斷是否需要組件更新, 默認(rèn) true
            shouldComponentUpdate(nextProps, nextState) {
                console.log('判斷組件是否需要更新')
                // 返回 false 時, 后續(xù)函數(shù)不執(zhí)行
                return true
            }
            // 3. 組件將要更新
            componentWillUpdate(nextProps, nextState) {
                console.log('nextProps: ', nextProps)
                console.log('nextState: ', nextState)
                console.log('組件將要被掛載')
            }
            // 4. 組件被重新渲染
            render() {
                console.log('組件被重新渲染')
                return (
                    <h1>{this.props.content}</h1>
                );
            }
            // 5. 組件已經(jīng)更新
            componentDidUpdate() {
                console.log('組件已經(jīng)被掛載')
            }
        }
        class FatherComponent extends React.Component {
            constructor(props) {
                super(props)
                this.state = {
                    content: '生命周期展示'
                }
            }
            componentDidMount() {
                this.setState({
                    content: '生命周期展示被改變'
                })
            }
            render() {
                return (
                    <CycleLife content={this.state.content}/>
                )
            }
        }
        ReactDOM.render(
            <FatherComponent />,
            document.getElementById('root')
        );
    

文章首發(fā) learning-react,本文禁止轉(zhuǎn)載。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 這一部分內(nèi)容一直一知半解,最近發(fā)現(xiàn)一篇文章,非常好的解釋了生命周期的問題,留存在這里,以備后查! 簡介 一個rea...
    春木橙云閱讀 1,027評論 0 5
  • 上面的這幅圖已經(jīng)很好地詮釋一個react組件的生命周期,所以認(rèn)真看圖!認(rèn)真看圖!認(rèn)真看圖! 一、getDefaul...
    好大一顆星閱讀 356評論 0 2
  • 深入JSX date:20170412筆記原文其實JSX是React.createElement(componen...
    gaoer1938閱讀 8,182評論 2 35
  • 1 React生命周期流程 調(diào)用流程可以參看上圖。分為實例化,存在期和銷毀三個不同階段。介紹生命周期流程的文章很多...
    Dabao123閱讀 390評論 0 1
  • 作為一個輕量級前端組件框架,react流行起來也不無道理,它可以不同程度的適配到你的應(yīng)用中。你既可以只是輕量的引入...
    大路無疆閱讀 1,026評論 0 8

友情鏈接更多精彩內(nèi)容