React@15.6.2源码解析---从 ReactDOM.render 到页面渲染(1)ReactMount

本文详细解析React 15.6.2版本的源码,从ReactDOM.render开始,深入ReactMount模块,包括ReactMount._renderSubtreeIntoContainer和ReactMount._renderNewRootComponent方法。通过阅读源码,理解React组件的渲染过程和关键函数的执行流程。建议在Chrome中使用断点辅助理解。
摘要由CSDN通过智能技术生成

title: React@15.6.2源码解析—从 ReactDOM.render 到页面渲染(1)ReactMount
tags:
- js
- react

之前介绍了React16.8版本的React公用API,本着学习最新版的React的想法,但是败在了Fiber的阵下,还有回过头来写搞明白React15的源码,毕竟从15到16是一次重大的更新。本文中React源码版本为 15.6.2 ,望各位看官找准版本号,不同的版本还是有着细微的区别的

值得一提的是,在阅读源码时,在Chrome中打断点是一个很好的操作,可以了解到函数的调用栈,变量的值,一步一步的调试还可以了解整个执行的流程,一边调试一边记录着流程一边在加以理解一边感慨这神乎其技的封装。

博客会同步到github上,这样也算是有了开源的项目。欢迎各位看官指教!

准备步骤

首先需要安装 React@15.6.2, ReactDOM@15.6.2 ,其次搭建webpack打包,因为必不可少的需要console.log啥的,另外需要babel的配置,babel6 babel7倒是无所谓,关键是可以解析我们的jsx语法。

示例代码

import React from 'react';
import ReactDom from 'react-dom';

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            name: 'Hello World'
        }
    }
    componentWillMount() {
        console.log('component will mount');
        this.setState({
            name: 'Hello CHina'
        })
    }
    componentDidMount() {
        console.log('component did mount');
        this.setState({
            name: 'Hello CHange'
        })
    }
    componentWillReceiveProps(nextProps) {
        console.log('component will receive props');
    }
    componentWillUpdate(nextProps, nextState) {
        console.log('component will updates');
    }
    componentDidUpdate(prevProps, prevState){
        console.log('component Did Update');
    };
    render() {
        console.log('render');
        return (
            <div>
                { this.state.name }
            </div>
        )
    }
};

ReactDom.render(
    <App>
        <div>Hello World</div>
    </App>,
    document.getElementById('root')
);

本片博客就是基于该代码进行调试的,将这段代码使用babel转码之后的结果为

// 主要代码段
var App =
  /*#__PURE__*/
  function (_React$Component) {
    _inherits(App, _React$Component);

    function App(props) {
      var _this;

      _classCallCheck(this, App);

      _this = _possibleConstructorReturn(this, _getPrototypeOf(App).call(this, props));
      _this.state = {
        name: 'Hello World'
      };
      return _this;
    }

    _createClass(App, [{
      key: "componentWillMount",
      value: function componentWillMount() {
        console.log('component will mount');
        this.setState({
          name: 'Hello CHina'
        });
      }
    }, {
      key: "componentDidMount",
      value: function componentDidMount() {
        console.log(&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React 是一个用于构建用户界面的 JavaScript 库,它是由 Facebook 团队开发和维护的。React 可以帮助我们快速、高效地构建交互式用户界面,而不需要担心如何管理底层的 DOM 操作。 在 React 中,UI 是通过组件来构建的。组件是一种可复用的代码块,可以封装 HTML、CSS 和 JavaScript 代码,并且可以在其他地方重复使用。React 组件被设计为具有单向数据流,即从父组件流向子组件,保证了组件的可预测性和可维护性。 在 React 中,我们使用 ReactDOM.render() 函数来将组件渲染页面上。该函数接受两个参数:第一个参数是要渲染的组件,第二个参数是要将组件渲染到的 DOM 元素。 例如,下面是一个简单的 React 组件和它的渲染代码: ```javascript // 定义一个简单的组件 function Greeting(props) { return <h1>Hello, {props.name}!</h1>; } // 渲染组件到页面ReactDOM.render( <Greeting name="World" />, document.getElementById('root') ); ``` 在上面的代码中,我们定义了一个名为 Greeting 的组件,它接受一个名为 name 的属性,并返回一个包含该属性值的 h1 元素。然后,我们使用 ReactDOM.render() 函数将该组件渲染页面上,将其放置在具有 id 为 root 的 DOM 元素中。 当我们运行该代码时,React 将会把 Greeting 组件渲染成一个 h1 元素,显示出 "Hello, World!" 的文本。这是 React 入门的第一步,我们可以从这里开始探索更多的 React 组件和功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值