[React] render中进行diff

场景

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class A extends Component {
    componentWillMount() {
        console.log('A: componentWillMount');
    }

    render() {
        console.log('A: render');

        return (
            <div>
                <div>456</div>
                <div>A{this.props.a}</div>
            </div>
        );
    }

    componentDidMount() {
        console.log('A: componentDidMount');
    }

    componentWillReceiveProps() {
        console.log('A: componentWillReceiveProps');
    }

    shouldComponentUpdate() {
        console.log('A: shouldComponentUpdate');
        return true;
    }

    componentWillUpdate() {
        console.log('A: componentWillUpdate');
    }

    // render

    componentDidUpdate() {
        console.log('A: componentDidUpdate');
    }
}

class Page extends Component {
    state = {
        a: 1
    };

    componentWillMount() {
        console.log('Page: componentWillMount');
    }

    render() {
        console.log('Page: render');

        return (
            <div>
                <div>123</div>
                <A a={this.state.a} />
            </div>
        );
    }

    componentDidMount() {
        console.log('Page: componentDidMount');

        setTimeout(() => {
            console.warn('Page: setState');
            this.setState({
                a: 1
            });
        }, 2000);
    }

    componentWillReceiveProps() {
        console.log('Page: componentWillReceiveProps');
    }

    shouldComponentUpdate() {
        console.log('Page: shouldComponentUpdate');
        return true;
    }

    componentWillUpdate() {
        console.log('Page: componentWillUpdate');
    }

    // render

    componentDidUpdate() {
        console.log('Page: componentDidUpdate');
    }
}

ReactDOM.render(
    <Page />,
    document.getElementById('app')
);

解答

1. 日志分析

(1)即使没有改变state,也会调用shouldComponentUpdate
this.setState({a:2});(改变了state)和this.setState({a:1});(没有改变state)日志结果一样。

// 当前组件和子组件shouldComponentUpdate都为true

Page: componentWillMount
Page: render
    A: componentWillMount
    A: render
    A: componentDidMount
Page: componentDidMount

Page: setState
Page: shouldComponentUpdate ---- true
Page: componentWillUpdate
Page: render
    A: componentWillReceiveProps
    A: shouldComponentUpdate ---- true
    A: componentWillUpdate
    A: render
    A: componentDidUpdate
Page: componentDidUpdate

(2)如果A组件的shouldComponentUpdate返回false
那么A组件的componentWillUpdate render componentDidUpdate就都不执行了。

// 子组件shouldComponentUpdate为false

Page: componentWillMount
Page: render
    A: componentWillMount
    A: render
    A: componentDidMount
Page: componentDidMount

Page: setState
Page: shouldComponentUpdate ---- true
Page: componentWillUpdate
Page: render
    A: componentWillReceiveProps
    A: shouldComponentUpdate ---- false
Page: componentDidUpdate

注:这一点只是在当前React版本中生效

Currently, if shouldComponentUpdate() returns false, then componentWillUpdate(), render(), and componentDidUpdate() will not be invoked. Note that in the future React may treat shouldComponentUpdate() as a hint rather than a strict directive, and returning false
may still result in a re-rendering of the component.
—— React.Component: shouldComponentUpdate()

(3)如果组件PageshouldComponentUpdate返回false
那么Page组件的componentWillUpdate render componentDidUpdate就都不执行了。

// 当前组件的shouldComponentUpdate为false

Page: componentWillMount
Page: render
    A: componentWillMount
    A: render
    A: componentDidMount
Page: componentDidMount

Page: setState
Page: shouldComponentUpdate ---- false

注意,A组件的componentWillReceiveProps shouldComponentUpdate componentWillUpdate render componentDidUpdate也都不执行了。
因为,子组件的componentWillReceiveProps是在父组件render后执行的,子组件componentDidUpdate后,父组件才会componentDidUpdate

2. DOM更新

在调试工具中查看哪些DOM被重新渲染
(1)打开chrome开发者工具
(2)按Esc,打开console
(3)点击console左边的按钮,勾选Rendering
(4)勾选Paint Flashing


我们发现,即使render函数被调用,DOM也不是全部更新,而是根据diff算法来更新。

3. 结论

只要执行this.setState,则当前组件的shouldComponentUpdate就会被调用。

如果当前组件的shouldComponentUpdate返回true
则子组件的componentWillReceiveProps shouldComponentUpdate将被调用,不论子组件的props是否被改变
如果当前组件的shouldComponentUpdate返回false
则子组件的componentWillReceiveProps shouldComponentUpdate componentWillUpdate render componentDidUpdate被调用。

如果子组件的shouldComponentUpdate返回true,则调用componentWillUpdate render,然后通过diff算法更新DOM,最后调用componentDidUpdate
如果子组件的shouldComponentUpdate返回false,则子组件的componentWillUpdate render componentDidUpdate都不被调用。


参考

React.Component: The Component Lifecycle
Reconciliation: The Diffing Algorithm

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 原教程内容详见精益 React 学习指南,这只是我在学习过程中的一些阅读笔记,个人觉得该教程讲解深入浅出,比目前大...
    leonaxiong阅读 2,960评论 1 18
  • 深入JSX date:20170412笔记原文其实JSX是React.createElement(componen...
    gaoer1938阅读 8,192评论 2 35
  • 自己最近的项目是基于react的,于是读了一遍react的文档,做了一些记录(除了REFERENCE部分还没开始读...
    潘逸飞阅读 3,761评论 1 10
  • react 基本概念解析 react 的组件声明周期 react 高阶组件,context, redux 等高级...
    南航阅读 1,143评论 0 1
  • It's a common pattern in React to wrap a component in an ...
    jplyue阅读 3,427评论 0 2

友情链接更多精彩内容