当前位置:主页 > 查看内容

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

发布时间:2021-09-25 00:00| 位朋友查看

简介:在开始开发之前,我们需要创建一个空项目文件夹。 安装 初始化 npminit-y 2.安装webpack相关依赖 npminstallwebpackwebpack-cli-D 3.安装babel-loader相关依赖 npminstallbabel-loader@babel/core@babel/preset-env-D 4.安装jsx支持依赖 npminstall@babel/pl……

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

安装

初始化

  1. npm init -y 

2.安装webpack相关依赖

  1. npm install webpack webpack-cli -D 

3.安装babel-loader相关依赖

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

4.安装jsx支持依赖

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

配置

1.在根目录下创建main.js文件 此文件为入口文件。

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

  1. module.exports={ 
  2.   entry:{ 
  3.     main:'./main.js' 
  4.   }, 
  5.   module:{ 
  6.     rules:[ 
  7.       { 
  8.         test:/\.js$/, 
  9.         use:{ 
  10.           loader:'babel-loader'
  11.           options:{ 
  12.             presets:['@babel/preset-env'], 
  13.             plugins:[['@babel/plugin-transform-react-jsx',{pragma:'createElement'}]] // 自定义设置pragma参数,我也可以设置为我的名字:maomin 
  14.           } 
  15.         } 
  16.       } 
  17.     ] 
  18.   }, 
  19.   mode:'development'
  20.   optimization:{ 
  21.     minimize: false 

3.创建一个reactJsx.js文件 此文件为主要逻辑文件。

开发

reactJsx.js

  1. // 封装创建Dom节点 
  2. class ElementWrapper { 
  3.   constructor(type) { 
  4.     this.root = document.createElement(type); 
  5.   } 
  6.   setAttibute(name, value) { 
  7.     this.root.setAttibute(name, value); 
  8.   } 
  9.   appendChild(component) { 
  10.     this.root.appendChild(component.root); 
  11.   } 
  12.  
  13. // 封装插入文本节点 
  14. class TextWrapper { 
  15.   constructor(content) { 
  16.     this.root = document.createTextNode(content); 
  17.   } 
  18. // 组件 
  19. export class Component { 
  20.   constructor() { 
  21.     this.props = Object.create(null); // 创建一个原型为null的空对象 
  22.     this.children = []; 
  23.     this._root = null
  24.   } 
  25.   setAttribute(name, value) { 
  26.     this.props[name] = value; 
  27.   } 
  28.   appendChild(component) { 
  29.     this.children.push(component); 
  30.   } 
  31.   get root() { // 取值 
  32.     if (!this._root) { 
  33.       this._root = this.render().root; 
  34.     } 
  35.     return this._root; 
  36.   } 
  37. // 创建节点,createElement对照 webapck.config.js 中pragma参数。 
  38. export function createElement(type, attributes, ...children) { 
  39.   let e; 
  40.   if (typeof type === "string") { 
  41.     e = new ElementWrapper(type); 
  42.   } else { 
  43.     e = new type(); 
  44.   } 
  45.   for (let p in attributes) { // 循环属性 
  46.     e.setAttribute(p, attributes[p]); 
  47.   } 
  48.   let insertChildren = (children) => { 
  49.     for (let child of children) { 
  50.       if (typeof child === "string") { 
  51.         child = new TextWrapper(child); 
  52.       } 
  53.       if (typeof child === "object" && child instanceof Array) { 
  54.         insertChildren(child); // 递归 
  55.       } else { 
  56.         e.appendChild(child); 
  57.       } 
  58.     } 
  59.   }; 
  60.   insertChildren(children); 
  61.   return e; 
  62.  
  63. // 添加到Dom中 
  64. export function render(component, parentElement) { 
  65.   parentElement.appendChild(component.root); 

main.js

import {createElement,Component,render} from './reactJsx.js'class MyComponent extends Component { render(){ return

maomin

  1. import {createElement,Component,render} from './reactJsx.js' 
  2.  
  3. class MyComponent extends Component { 
  4.   render(){ 
  5.     return <div> 
  6.       <h1>maomin</h1> 
  7.       {this.children} 
  8.     </div> 
  9.   } 
  10.  
  11. render(<MyComponent id="name" class="age"
  12.   <div>xqm</div> 
  13.   <div>my girlfriend</div> 
  14. </MyComponent>,document.body) 
执行
本文转载自网络,原文链接:https://mp.weixin.qq.com/s/x4qwEcSZ0vG4EaHiA3O0hA
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:前端: 用Javascript实现一个转盘小游戏? 下一篇:没有了

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐