1.react入门-JSX

循循善诱
• 阅读 1031
  1. ReactDom
    ReactDom是一个js对象,是用户写的ReactDOM和浏览器中的DOM之间的映射表,用以构建 DOM 以及保持随时更新。

    import React, { Component } from 'react';
    import { render } from 'react-dom';
    
    let element = <h1 id="title">hello<h2>word</h2></h1>; 
    console.log(element)
    render(element, document.getElementById('root'));

    babel会将以上代码编译成:

    var element = React.createElement("h1", {
      id: "title"
    }, "hello", React.createElement("h2", null, "word"));

    React.createElement的实现原理基本如下:

    function createElement(type, options={}, ...children){
        return {
            type,
            props: {...options, children}
        }
    }

    这样,element就是一个含有type、props这两个核心属性的嵌套对象。

  2. JSX的Tips
    根据JSX渲染引擎原理,JSX语法需要注意以下几点:

    1. 避免使用js关键字,并使用小驼峰规则,如class --> className;
    2. 为了高效DOM diff,列表中要加上key属性;
    3. ReactDom是不可变对象,不可以通过修改props属性来重新渲染DOM;
    4. 重复渲染也会进行DOMdiff。会尽可能复用原有DOM,只更新必要DOM。
  3. React.createElement基本原理

    const hasSymbol = typeof symbol == "function" && symbol.for;
    const REACT_ELEMENT_TYPE = hasSymbol ? Symbol.for("react.element") : 0xeac7;
    
    function createElement(type, config, children) {
        let props = {};
        for (let key in config) {
            props[key] = config[key];
        }
        const childrenLength = arguments.length - 2;
        if (childrenLength == 1) {
            props.children = children;
        } else if (childrenLength > 1) {
            props.children = Array.prototype.slice.call(arguments, 2);
        }
        return { $$typeof: REACT_ELEMENT_TYPE, type, props };
    }
    
    class Component {
        constructor(props) {
            this.props = props;
        }
        static isReactComponent = true;
    }
    
    export default { createElement, Component };
  4. ReactDom.render的基本原理

    /**
     *@param {*} node React节点,可能的值为:React元素、数字、字符串
     *@param {*} parent 父容器,是一个真实DOM元素
     */
    function render(node, parent) {
        if (typeof node === "string") {
            return parent.appendChild(document.createTextNode(node));
        }
        let type, props;
        type = node.type;
        props = node.props;
        if(type.isReactComponent){//如果是类组件
            let element = new type(props).render();
            type = element.type;
            props = element.props;
            if(typeof element.type == 'function'){//如果类组件返回的是函数组件
                return render(element, parent)
            }
        }else if (typeof type === "function") {//如果是函数组件~~~~
            let element = type(props);
            type = element.type;
            props = element.props;
            if(typeof element.type == 'function'){
                return render(element, parent)
            }
        }
        let domElement = document.createElement(type); //创建真实的DOM元素
        for (let propName in props) {
            if (propName == "children") { // 如果是children,则递归遍历
                let children = props.children;
                if (!Array.isArray(children)) {//如果不是数组,则变为数组
                    children = [children];
                }
                children.forEach(child => render(child, domElement));
            } else if (propName === "className") {
                domElement.className = props.className;
            } else if (propName === "style") {
                let styleObject = props.style;
                for (let attr in styleObject) domElement.style[attr] = styleObject[attr];
            } else {
                domElement.setAttribute(propName, props[propName]);
            }
        }
        parent.appendChild(domElement);
    }
    export default {
        render
    };
点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
4年前
SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器!复制代码(https://oscimg.oschina.net/oscnet/675f5689159acfa2c39c91f4df40a00ce0f.gif)/cookie对象;rememberMeCookie()方法是设置Cookie的生成模
Wesley13 Wesley13
4年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Stella981 Stella981
4年前
React应用渲染界面的入口
jsx代码:varReactrequire('react');varReactDOMrequire('reactdom');varMyButtonControllerrequire('./components/MyButtonController');ReactDOM.render
Wesley13 Wesley13
4年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Easter79 Easter79
4年前
SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器!复制代码(https://oscimg.oschina.net/oscnet/675f5689159acfa2c39c91f4df40a00ce0f.gif)/cookie对象;rememberMeCookie()方法是设置Cookie的生成模
Wesley13 Wesley13
4年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Python进阶者 Python进阶者
2年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这