美文网首页React.js
React 父子组建生命周期

React 父子组建生命周期

作者: 醉生夢死 | 来源:发表于2017-03-16 22:25 被阅读1053次

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 正常的执行顺序会是

st=>operation: constructor
e=>operation: componentDidMount
op=>operation: componentWillMount
cond=>operation: render

st->op->cond->e

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


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

//根组件
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

相关文章

  • React 父子组建生命周期

    React 父子组建生命周期 React 和 ReactNative 是亲兄弟,都是 facebook的儿子,只不...

  • React.JS 学习资源汇——持续更新

    React 学习目标 能够熟练运用React框架 了解React组建的生命周期 入门学习 官方文档英文原版http...

  • react官网阅读笔记

    React react支持IE9+ 组建的生命周期 挂载 getInitialState es5中使用的初始化状态...

  • React生命周期的触发

    前言 react生命周期老生常谈了,但是父子组件的生命周期是什么样的?调用setState之后的生命周期又会怎样,...

  • React组建的生命周期

    React 生命周期 React生命周期主要包括4个阶段: 初始化阶段 实例化阶段 更新阶段 销毁阶段 1 设置组...

  • 5

    数据的获取 这章的目标是不同的数据获取方式,为了找到最好的方式,我们组好理清React组建树的数据流,父子组建是如...

  • [FE] componentWillReceiveProps陷阱

    1. 生命周期 React父子组件体系中,首次渲染的生命周期函数,触发情况如下, 等渲染完后,在父组件的compo...

  • vue 父子组建生命周期调用顺序

    index 复组建 list 子组建 index created list created list mounte...

  • React概念图

    React概念图 React组件生命周期概念图 参考文档:React入门教程 组件生命周期React:组件生命周期...

  • React基础篇之组件的生命周期

    引出生命周期 react生命周期(旧) react生命周期(新) getSnapshotBeforeUpdate的...

网友评论

    本文标题:React 父子组建生命周期

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