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

React 生命周期函数

作者: 幸宇 | 来源:发表于2019-07-31 19:18 被阅读0次

https://reactjs.org/docs/react-component.html

React生命周期函数:

组件加载之前,组件加载完成,以及组件更新数据,组件销毁。

触发的一系列的方法 ,这就是组件的生命周期函数

组件加载的时候触发的函数:

constructor 、componentWillMount、 render 、componentDidMount

组件数据更新的时候触发的生命周期函数:

shouldComponentUpdate、componentWillUpdate、render、componentDidUpdate

你在父组件里面改变props传值的时候触发的:

componentWillReceiveProps

组件销毁的时候触发的:

componentWillUnmount

必须记住的生命周期函数:

*加载的时候:componentWillMount、 render 、componentDidMount(dom操作)

更新的时候:componentWillUpdate、render、componentDidUpdate

*销毁的时候: componentWillUnmount
函数使用
import React, { Component } from 'react';

class Lifecycle extends Component {
    constructor(props) {

        console.log('01构造函数');
        super(props);
        this.state = { 

            msg:'我是一个msg'
         };
    }  

    //组件将要挂载的时候触发的生命周期函数
    componentWillMount(){

        console.log('02组件将要挂载');
    }
    //组件挂载完成的时候触发的生命周期函数
    componentDidMount(){

        //dom操作放在这个里面    请求数据也放在这个里面

        console.log('04组件将要挂载');
    }


    //是否要更新数据  如果返回true才会执行更新数据的操作
    shouldComponentUpdate(nextProps, nextState){
        console.log('01是否要更新数据');

        console.log(nextProps);

        console.log(nextState);

        return true;
    }

    //将要更新数据的时候触发

    componentWillUpdate(){
        console.log('02组件将要更新');
    }
    //组件更新完成
    componentDidUpdate(){
        console.log('04组件数据更新完成');
    }

    // 你在父组件里面改变props传值的时候触发的

    componentWillReceiveProps(){

        console.log('父子组件传值,父组件里面改变了props的值触发的方法')
    }

    setMsg=()=>{

        this.setState({

            msg:'我是改变后的msg的数据'
        })
    }

    //组件销毁的时候触发的生命周期函数   用在组件销毁的时候执行操作
    componentWillUnmount(){

            console.log('组件销毁了');
    }
    render() {
        console.log('03数据渲染render');
       
        return (
            <div>

                生命周期函数演示--- {this.state.msg}-----{this.props.title}

                <br />
                <br />

                <button onClick={this.setMsg}>更新msg的数据</button>
            </div>
        );
    }
}

export default Lifecycle;
组件销毁

相关文章

  • 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/moqxdctx.html