美文网首页
React 生命周期函数

React 生命周期函数

作者: 马小帅mm | 来源:发表于2018-12-31 15:27 被阅读0次

一个组件从创建,更新,销毁会经历的一系列函数叫做生命周期函数。
具体可以参考官方文档

1.装载组件触发4个函数

  • constructor
  • componentWillMount
  • render
  • componentDidMount
import React, { Component } from 'react';

class Liftcycle extends Component {
    constructor(props){
        super(props);
        this.state = {
            title: '我是生命周期页面'
        }
        console.log('1.我是构造函数constructor');
    }
    componentWillMount(){
        console.log('2.我是组件挂载前的函数componentWillMount')
    }
    componentDidMount(){
        console.log('4.我是组件挂载后的函数componentDidMount')
    }
    render(){
        console.log('3.我是渲染函数render')
        return (
            <div>
                <div>{this.state.title}</div>
            </div>
        )
    }
}

export default Liftcycle;

运行后会得到下面的结果:


image.png

2.更新组件触发4个函数

  • shouldComponentUpdate
  • componentWillUpdate
  • render
  • componentDidUpdate
import React, { Component } from 'react';

class Liftcycle extends Component {
    constructor(props){
        super(props);
        this.state = {
            title: '我是生命周期页面'
        }
    }
    updataData = () => {
        this.setState({
            title: '我是更新后的生命周期页面'
        });
    }
    shouldComponentUpdate(){
        console.log('1.我是询问组件数据是否需要更新的函数(return true;更新;return false;不更新;)shouldComponentUpdate')
        return true;
    }
    componentWillUpdate(){
        console.log('2.我是组件数据更新前的函数componentWillUpdate')
    }
    componentDidUpdate(){
        console.log('4.我是组件数据更新后的函数componentDidUpdate')
    }
    render(){
        console.log('3.我是渲染函数render')
        return (
            <div>
                <div>{this.state.title}</div>
                <button onClick={this.updataData}>更新组件</button>
            </div>
        )
    }
}

export default Liftcycle;

点击按钮更新组件数据后,执行的顺序是:


image.png

3.卸载组件触发

  • componentWillUnmount
    可以在这个函数上做一些清理数据,缓存数据的操作
componentWillUnmount(){
    console.log('我是卸载组件触发了的函数');
}

4.父组件更新传递给子组件的值时触发

  • componentWillUnmount
    该方法在子组件里书写,当父组件更新了传递给子组件的值时会触发
componentWillReceiveProps(){
    console.log('我是父组件更新了传递给我的值触发的函数')
}

END---------

相关文章

  • RN-生命周期函数(新)

    旧版生命周期函数 react 16.4 以后生命周期函数 http://projects.wojtekmaj.pl...

  • React 生命周期函数

    https://reactjs.org/docs/react-component.html React生命周期函数...

  • React的生命周期函数

    一、生命周期函数的定义 在某个时刻自动被调用的函数是生命周期函数 二、React中生命周期函数示意图 三、生命周期...

  • 04.React高级部分(中)

    React的生命周期函数 React生命周期函数的使用场景 这一小节,我们来做两个生命周期相关的常用操作1.如何避...

  • useState

    react-hooks 如果你熟悉 React class 的生命周期函数,你可以把 useEffect Hook...

  • 四:React 进阶三 (生命周期)

    react(一):组件的生命周期 生命周期函数 在组件运行的某个时刻,会被自动执行的一些函数。 React生命周期...

  • React快速上手5-react中的事件和生命周期函数

    这节中我们将主要介绍react应用中的事件和生命周期函数 React事件 在react中,我们不用addEvent...

  • react生命周期函数

    react 生命周期函数介绍[https://www.cnblogs.com/kdcg/p/9182393.htm...

  • 2020-09-11

    REACT生命周期 (JS胖老师课程 ) 生命周期函数指在某一个时刻组件会自动调用执行的函数 REACT生命周期的...

  • React 生命周期

    React 16.4 的生命周期图 早期React生命周期图 从图中,我们看到了一些变化 废弃的三个生命周期函数 ...

网友评论

      本文标题:React 生命周期函数

      本文链接:https://www.haomeiwen.com/subject/gwailqtx.html