React子父组件生命周期函数执行顺序

react生命周期函数不做太多描述

App.jsx:

    constructor(props){
        super(props);
        console.log("App constructor")
    }
    
    componentWillMount(){
        console.log("App componentWillMount")
    }
    
    componentDidMount(){
        console.log("App componentDidMount")
    }
    render(){
        console.log("App render")
        return <div>Apppppppppp
            <A></A>
        </div>
    }

A.jsx:

    constructor(props){
        super(props);
        console.log("A constructor")
    }
    
    componentWillMount(){
        console.log("A componentWillMount")
    }
    
    componentDidMount(){
        console.log("A componentDidMount")
    }
    render(){
        console.log("A render")
        return <div>AAAA</div>
    }

输出结果:
App constructor
App componentWillMount
App render
A constructor
A componentWillMount
A render
A componentDidMount
App componentDidMount

也就是父组件在内存中生成页面树的时候先去制作子组件,等到子组件挂在到父组件某个节点时在继续内存渲染父组件知道父组件didmount.

多个子组件的情况

App.jsx:

    constructor(props){
        super(props);
        console.log("App constructor")
    }
    
    componentWillMount(){
        console.log("App componentWillMount")
    }
    
    componentDidMount(){
        console.log("App componentDidMount")
    }
    render(){
        console.log("App render")
        return <div>Apppppppppp
            <A></A>
            <B></B>
        </div>
    }

A.jsx不变 添加B.jsx:

    constructor(props){
        super(props);
        console.log("B constructor")
    }
    
    componentWillMount(){
        console.log("B componentWillMount")
    }
    
    componentDidMount(){
        console.log("B componentDidMount")
    }
    render(){
        console.log("B render")
        return <div>BBBB</div>
    }

结果输出:
App constructor
App componentWillMount
App render
A constructor
A componentWillMount
A render
B constructor
B componentWillMount
B render
A componentDidMount
B componentDidMount
App componentDidMount

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值