React 父子组建生命周期

React 父子组建生命周期

ReactReactNative 是亲兄弟,都是 facebook的儿子,只不过ReactNative拥有自己的一套标签库,因此他们的生命周期一样,要讲React的生命周期的话,网上一搜一大堆,很多比我写得还好。


我先附上React 生命周期的图,然后再进入主题。

这里写图片描述


我今天要讲的是 React 父子组建组合的时候,React 生命周期的变化。事实上我们做 React / React Native 开发时,免不了会使用多个组件组合使用。例如下面这段代码。

class App extends Component {

    render() {
        return (
            <View>
                <Button>login</Button>
            </View>
        );
    }

}

class Button extends Component {
    render() {
        return (
            <TouchableOpacity>
                {this.props.children}
            </TouchableOpacity>
        );
    }
}

根据上面的 React 生命周期图来看,一个 React 正常的执行顺序会是

Created with Raphaël 2.1.0 constructor componentWillMount render componentDidMount

现在我是父组建和子组件进行整合,他的执行顺序会是怎样呢?
这里大家可以先思考下。


我写了代入如下来验证结果

//根组件
class RootContainer extends Component {

    constructor(props) {
        super(props);
        console.log('RootContainer constructor');
    }

    componentWillMount() {
        console.log('RootContainer componentWillMount');
    }

    render() {
        console.log('RootContainer render');
        return (
            <div className="root">
                <h3>This is RootContainer</h3>
                <ChildView/>
            </div>
        );
    }

    componentDidMount() {
        console.log('RootContainer componentDidMount');
    }

    componentWillUnmount() {
        console.log('RootContainer componentWillUnmount');
    }

    componentWillReceiveProps(nextProps) {
        console.log('RootContainer componentWillReceiveProps(nextProps)');
    }

    componentWillUpdate(nextProps, nextState) {
        console.log('RootContainer componentWillUpdate(nextProps, nextState)');
    }

    shouldComponentUpdate(nextProps, nextState) {
        console.log('RootContainer shouldComponentUpdate(nextProps, nextState)');
        return true;
    }

    componentDidUpdate(prevProps, prevState) {
        console.log('RootContainer componentDidUpdate(prevProps, prevState)');
    }

}

//子组件
class ChildView extends Component {

    constructor(props) {
        super(props);
        console.log('ChildView constructor');
    }

    componentWillMount() {
        console.log('ChildView componentWillMount');
    }

    render() {
        console.log('ChildView render');
        return (
            <div className="child">
                <h4>I am a Child</h4>
            </div>
        );
    }

    componentDidMount() {
        console.log('ChildView componentDidMount');
    }

    componentWillUnmount() {
        console.log('ChildView componentWillUnmount');
    }

    componentWillReceiveProps(nextProps) {
        console.log('ChildView componentWillReceiveProps(nextProps)');
    }

    shouldComponentUpdate(nextProps, nextState) {
        console.log('ChildView shouldComponentUpdate(nextProps, nextState)');
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
        console.log('ChildView componentWillUpdate(nextProps, nextState)');
    }

    componentDidUpdate(prevProps, prevState) {
        console.log('ChildView componetDidUpdate(prevProps, prevState)');
    }

}

运行之后,控制台输出语句如下

RootContainer constructor
RootContainer componentWillMount
RootContainer render
— ChildView constructor
— ChildView componentWillMount
— ChildView render
— ChildView componentDidMount
RootContainer componentDidMount

此时可以分析出,当父组建 render 时遇到子组件,然后进入子组件的生命周期,当执行完子组件生命周期中的componentDidMount 时会回到父组建继续执行父组建未完成的生命周期。


更深级别的层级效果也是如此

RootContainer constructor
RootContainer componentWillMount
RootContainer render
— ChildView constructor
— ChildView componentWillMount
— ChildView render
—— Grandson constructor
—— Grandson componentWillMount
—— Grandson render
—— Grandson componentDidMount
— ChildView componentDidMount
RootContainer componentDidMount

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值