React.js中JSX的原理与关键实现

简介: 在开始开发之前,我们需要创建一个空项目文件夹

安装


  1. 初始化


npm init -y


2.安装webpack相关依赖


npm install webpack webpack-cli -D


  1. 安装babel-loader相关依赖


npm install babel-loader @babel/core @babel/preset-env -D


  1. 安装jsx支持依赖


npm install @babel/plugin-transform-react-jsx -D


配置


  1. 在根目录下创建main.js文件


此文件为入口文件。


  1. 在项目根目录下创建webpack.config.js


module.exports={
  entry:{
    main:'./main.js'
  },
  module:{
    rules:[
      {
        test:/\.js$/,
        use:{
          loader:'babel-loader',
          options:{
            presets:['@babel/preset-env'],
            plugins:[['@babel/plugin-transform-react-jsx',{pragma:'createElement'}]] // 自定义设置pragma参数,我也可以设置为我的名字:maomin
          }
        }
      }
    ]
  },
  mode:'development',
  optimization:{
    minimize: false
  }
}


  1. 创建一个reactJsx.js文件


此文件为主要逻辑文件。


开发


reactJsx.js


// 封装创建Dom节点
class ElementWrapper {
  constructor(type) {
    this.root = document.createElement(type);
  }
  setAttibute(name, value) {
    this.root.setAttibute(name, value);
  }
  appendChild(component) {
    this.root.appendChild(component.root);
  }
}
// 封装插入文本节点
class TextWrapper {
  constructor(content) {
    this.root = document.createTextNode(content);
  }
}
// 组件
export class Component {
  constructor() {
    this.props = Object.create(null); // 创建一个原型为null的空对象
    this.children = [];
    this._root = null;
  }
  setAttribute(name, value) {
    this.props[name] = value;
  }
  appendChild(component) {
    this.children.push(component);
  }
  get root() { // 取值
    if (!this._root) {
      this._root = this.render().root;
    }
    return this._root;
  }
}
// 创建节点,createElement对照 webapck.config.js 中pragma参数。
export function createElement(type, attributes, ...children) {
  let e;
  if (typeof type === "string") {
    e = new ElementWrapper(type);
  } else {
    e = new type();
  }
  for (let p in attributes) { // 循环属性
    e.setAttribute(p, attributes[p]);
  }
  let insertChildren = (children) => {
    for (let child of children) {
      if (typeof child === "string") {
        child = new TextWrapper(child);
      }
      if (typeof child === "object" && child instanceof Array) {
        insertChildren(child); // 递归
      } else {
        e.appendChild(child);
      }
    }
  };
  insertChildren(children);
  return e;
}
// 添加到Dom中
export function render(component, parentElement) {
  parentElement.appendChild(component.root);
}


main.js


import {createElement,Component,render} from './reactJsx.js'
class MyComponent extends Component {
  render(){
    return <div>
      <h1>maomin</h1>
      {this.children}
    </div>
  }
}
render(<MyComponent id="name" class="age">
  <div>xqm</div>
  <div>my girlfriend</div>
</MyComponent>,document.body)


执行


npx webpack


在dist文件夹下创建html文件,然后引入main.js,打开html文件就可以看到效果了。


微信截图_20220505193403.png


微信截图_20220505193403.png微信截图_20220505193403.png微信截图_20220505193403.png

相关文章
|
28天前
|
XML JavaScript 前端开发
React Jsx语法入门
【8月更文挑战第13天】React Jsx语法入门
32 4
|
3月前
|
Web App开发 前端开发 JavaScript
技术心得记录:瀑布流的布局原理分析(纯CSS瀑布流与JS瀑布流)
技术心得记录:瀑布流的布局原理分析(纯CSS瀑布流与JS瀑布流)
37 0
|
16天前
|
前端开发 算法 JavaScript
React原理之Diff算法
【8月更文挑战第24天】
|
16天前
|
缓存 JavaScript 前端开发
[译] Vue.js 内部原理浅析
[译] Vue.js 内部原理浅析
|
19天前
|
JavaScript 前端开发 安全
JS 混淆解析:JS 压缩混淆原理、OB 混淆特性、OB 混淆JS、混淆突破实战
JS 混淆解析:JS 压缩混淆原理、OB 混淆特性、OB 混淆JS、混淆突破实战
26 2
|
26天前
|
前端开发 JavaScript
React Server Component 使用问题之添加jsx的组件化能力,如何操作
React Server Component 使用问题之添加jsx的组件化能力,如何操作
|
1月前
|
前端开发 JavaScript 算法
如何学习react原理
【8月更文挑战第9天】 如何学习react原理
34 6
|
1月前
|
前端开发 JavaScript 开发者
React组件与JSX之间的区别是什么
【8月更文挑战第9天】 React组件与JSX之间的区别是什么
39 4
|
13天前
|
前端开发 Java UED
瞬间变身高手!JSF 与 Ajax 强强联手,打造极致用户体验的富客户端应用,让你的应用焕然一新!
【8月更文挑战第31天】JavaServer Faces (JSF) 是 Java EE 标准的一部分,常用于构建企业级 Web 应用。传统 JSF 应用采用全页面刷新方式,可能影响用户体验。通过集成 Ajax 技术,可以显著提升应用的响应速度和交互性。本文详细介绍如何在 JSF 应用中使用 Ajax 构建富客户端应用,并通过具体示例展示 Ajax 在 JSF 中的应用。首先,确保安装 JDK 和支持 Java EE 的应用服务器(如 Apache Tomcat 或 WildFly)。
25 0
|
15天前
|
缓存 开发框架 JavaScript
人人都能看懂的鸿蒙 “JS 小程序” 数据绑定原理 | 解读鸿蒙源码
人人都能看懂的鸿蒙 “JS 小程序” 数据绑定原理 | 解读鸿蒙源码