成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

React.JS中JSX的原理與關鍵實現

開發 前端
本篇和大家一起學習React.js中JSX的原理與關鍵實現

[[354631]]

 在開始開發之前,我們需要創建一個空項目文件夾。

安裝

初始化

  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) 
執行
  1. npx webpack 

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

 

責任編輯:姜華 來源: 前端歷劫之路
相關推薦

2025-01-17 09:29:42

2016-11-14 15:51:42

JavaScriptAngular.jsReact.js

2025-01-13 00:00:00

2017-03-28 21:03:35

代碼React.js

2021-09-14 18:33:39

React 數據交互

2018-06-21 16:03:25

Vue.jsReact.js框架

2015-12-31 10:14:54

React.js開發Web應用

2020-04-27 14:54:45

React開發

2022-06-08 08:03:51

React.jsReactJS 庫

2021-05-21 09:34:40

React React 17前端

2017-02-09 15:19:14

2024-01-26 08:06:43

2021-10-12 23:01:42

項目語法React

2023-11-26 18:02:00

ReactDOM

2022-05-03 21:18:38

Vue.js組件KeepAlive

2022-04-25 07:36:21

組件數據函數

2022-07-06 08:30:36

vuereactvdom

2021-04-25 05:31:33

React.js項目FastReactAp

2015-02-11 09:44:49

React.js緩存構建

2021-05-24 06:00:20

ReactJSXJS
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩一区二区在线观看 | 日日干天天干 | 欧美精品福利视频 | 日韩和的一区二区 | 男女一区二区三区 | 欧美激情视频一区二区三区在线播放 | 日韩国产在线 | 夜夜草视频 | 国产精品黄色 | 中文字幕综合 | 国产不卡在线观看 | 中文字幕在线观看精品 | 九一在线观看 | 国产亚洲精品精品国产亚洲综合 | 成人毛片在线观看 | av成年人网站 | 欧美中文字幕 | 精品国产欧美一区二区 | 在线国产一区二区三区 | 一区二区三区视频在线 | 精品自拍视频在线观看 | 亚洲在线免费观看 | 国产精品久久久久久久久免费相片 | 午夜国产 | 国产精品一区二区av | 精品videossex高潮汇编 | 日韩高清国产一区在线 | 99久久婷婷国产综合精品首页 | 福利视频网站 | www.狠狠干 | 久久精品小视频 | 国产精品一区二区三 | 国产精品久久国产精品久久 | 天天舔天天 | 一级毛片网| 一区二区三区四区不卡视频 | 91在线网站 | 国产高清免费视频 | 国产高清一区二区三区 | 亚洲色视频 | 国产亚洲精品综合一区 |