React.js中JSX的原理與關(guān)鍵實(shí)現(xiàn)

在開(kāi)始開(kāi)發(fā)之前,我們需要?jiǎng)?chuàng)建一個(gè)空項(xiàng)目文件夾。

安裝

  1. 初始化
npm init -y

2.安裝webpack相關(guān)依賴

npm install webpack webpack-cli -D
  1. 安裝babel-loader相關(guān)依賴
npm install babel-loader @babel/core @babel/preset-env -D
  1. 安裝jsx支持依賴
npm install @babel/plugin-transform-react-jsx -D

配置

  1. 在根目錄下創(chuàng)建main.js文件
    此文件為入口文件。

  2. 在項(xiàng)目根目錄下創(chuàng)建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'}]] // 自定義設(shè)置pragma參數(shù),我也可以設(shè)置為我的名字:maomin
          }
        }
      }
    ]
  },
  mode:'development',
  optimization:{
    minimize: false
  }
}
  1. 創(chuàng)建一個(gè)reactJsx.js文件
    此文件為主要邏輯文件。

開(kāi)發(fā)

reactJsx.js

// 封裝創(chuàng)建Dom節(jié)點(diǎn)
class ElementWrapper {
  constructor(type) {
    this.root = document.createElement(type);
  }
  setAttibute(name, value) {
    this.root.setAttibute(name, value);
  }
  appendChild(component) {
    this.root.appendChild(component.root);
  }
}

// 封裝插入文本節(jié)點(diǎn)
class TextWrapper {
  constructor(content) {
    this.root = document.createTextNode(content);
  }
}
// 組件
export class Component {
  constructor() {
    this.props = Object.create(null); // 創(chuàng)建一個(gè)原型為null的空對(duì)象
    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;
  }
}
// 創(chuàng)建節(jié)點(diǎn),createElement對(duì)照 webapck.config.js 中pragma參數(shù)。
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) { // 循環(huán)屬性
    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)

執(zhí)行

npx webpack

在dist文件夾下創(chuàng)建html文件,然后引入main.js,打開(kāi)html文件就可以看到效果了。




作者:Vam的金豆之路

主要領(lǐng)域:前端開(kāi)發(fā)

我的微信:maomin9761

微信公眾號(hào):前端歷劫之路