React源码(一): render createElement
创始人
2024-03-26 06:15:36
0

React源码——渲染(render && createElement)的简单实现

前言

当前市面上公司对React需求越来越大主, 对于React的源码学习必须提上日程

初始化项目

  1. React脚手架创建项目

    • 全局安装npm install -g create-react-app
    • 创建项目create-react-app My-React-App
    • 删除不需要的文件
  2. 目录文件如下
    在这里插入图片描述

  3. 运行热更新报错,解决方案React v17 热刷新 不起作用, 或者在package.json的脚本中加上 "start": "FAST_REFRESH=false react-scripts start",
    在这里插入图片描述

react创建以及分析创建虚拟dom,babel转化的原理babel试一试

  1. Jsx----->babel----->js方法------>React.createElement()------->vdom
    在这里插入图片描述

  2. React.createElement实现

// react.js
import get$$Type from './stants';/* @param type 元素类型* @param props 元素的配置对象* @param children 元素的子元素们*/
function createElement(type,props,children){const { key, ref, ...resprops } = props ? props : {};const len = arguments.length;if(len > 3){// 多个childrenresprops.children = Array.prototype.slice.call(arguments,2)} else if(len === 3)r// childrenresprops.children = children;}else{// 没有children// do nothing}return {$$typeof:get$$Type(type),key,ref,type,props: resprops}
}const React = {createElement
}export default  React
// stants.js
const REACT_TEXT = Symbol("react.TEXT");
const REACT_ELEMENT = Symbol("react.element");
const get$$Type = (type) =>{return type ? REACT_ELEMENT : REACT_TEXT;
}
export default get$$Type

编写reactDOM.render方法,实现想虚拟dom转化成真实的dom

  1. vdom转化成真实dom
  2. 给真实dom赋值属性
  3. 递归childrenDom
  4. 内容append到对应的容器
// react-dom.js
// 渲染真是dom到页面
function render(vdom,container){const reldom = createDom(vdom);container.appendChild(reldom)
}// 处理函数式组件
const mountFunctionComponent = (vdom) => {const { type ,props } = vdom;// 获取到函数的vdomlet functionVdom = type(props);return createDom(functionVdom)
}// 初始化以及更新属性
const updateOtherProps = (relDom,oldProps,newProps) => {if(newProps){for(let key in newProps){if(key === 'style'){for(let k in newProps[key]){relDom.style[k] = newProps[key][k]}} else {relDom[key] = newProps[key]}}}// 更新处理if(oldProps){// 旧得属性在新的属性中没有删除for(let key in oldProps){if(!newProps[key]){relDom[key] = null}}}
}const createDom = (vdom) => {let relDom;// 纯文本if(!vdom?.type){return document.createTextNode(vdom);}// ReactFragment if( typeof vdom?.type === 'symbol'){return document.createTextNode(vdom?.props?.children);}const { type, props: { children,...resProps } } = vdom;// 函数组件if(typeof type == 'function') {// functionComponent---> 变成vdomreturn mountFunctionComponent(vdom)} else {// dom 节点relDom = document.createElement(type);}// 赋值属性if(resProps){updateOtherProps(relDom,{},resProps)}// 递归childrenDomif(children){renderChildren(children,relDom);}return relDom;
}const renderChildren = (children,dom) => {// 多个childrenif(Array.isArray(children)){for(let index in children){render(children[index],dom)}} else {render(children,dom)}
}const ReactDom = { render }
export default ReactDom

使用

import React from './react';
import ReactDOM from './react-dom';
import './index.css';const  App = 
{ color:'peru',background:'mediumpurple' }}>{ color:'yellow' }}>app{ color:'plum' }}>7777

{ color:'orange' }}>111

{ color:'ghostwhite' }}>apps333<>6666
; ReactDOM.render(App, document.getElementById('root'));

效果

在这里插入图片描述

相关内容

热门资讯

不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
APK正在安装,但应用程序列表... 这个问题可能是由于以下原因导致的:应用程序安装的APK文件可能存在问题。设备上已经存在同名的应用程序...
安卓文字转语音tts没有声音 安卓文字转语音TTS没有声音的问题在应用中比较常见,通常是由于一些设置或者代码逻辑问题导致的。本文将...
报告实验.pdfbase.tt... 这个错误通常是由于找不到字体文件或者文件路径不正确导致的。以下是一些解决方法:确认字体文件是否存在:...