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

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

站在用户的角度思考问题,与客户深入沟通,找到莫力达网站设计与莫力达网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、成都网站设计、企业官网、英文网站、手机端网站、网站推广、国际域名空间、雅安服务器托管、企业邮箱。业务覆盖莫力达地区。

安装

初始化

 
 
 
 
  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. // 组件
  20. export class Component {
  21.   constructor() {
  22.     this.props = Object.create(null); // 创建一个原型为null的空对象
  23.     this.children = [];
  24.     this._root = null;
  25.   }
  26.   setAttribute(name, value) {
  27.     this.props[name] = value;
  28.   }
  29.   appendChild(component) {
  30.     this.children.push(component);
  31.   }
  32.   get root() { // 取值
  33.     if (!this._root) {
  34.       this._root = this.render().root;
  35.     }
  36.     return this._root;
  37.   }
  38. }
  39. // 创建节点,createElement对照 webapck.config.js 中pragma参数。
  40. export function createElement(type, attributes, ...children) {
  41.   let e;
  42.   if (typeof type === "string") {
  43.     e = new ElementWrapper(type);
  44.   } else {
  45.     e = new type();
  46.   }
  47.   for (let p in attributes) { // 循环属性
  48.     e.setAttribute(p, attributes[p]);
  49.   }
  50.   let insertChildren = (children) => {
  51.     for (let child of children) {
  52.       if (typeof child === "string") {
  53.         child = new TextWrapper(child);
  54.       }
  55.       if (typeof child === "object" && child instanceof Array) {
  56.         insertChildren(child); // 递归
  57.       } else {
  58.         e.appendChild(child);
  59.       }
  60.     }
  61.   };
  62.   insertChildren(children);
  63.   return e;
  64. }
  65. // 添加到Dom中
  66. export function render(component, parentElement) {
  67.   parentElement.appendChild(component.root);
  68. }

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. class MyComponent extends Component {
  3.   render(){
  4.     return 
  5.       

    maomin

  6.       {this.children}
  7.     
  •   }
  • }
  • render(
  •   
    xqm
  •   
    my girlfriend
  • ,document.body)
  • 执行

     
     
     
     
    1. npx webpack

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

    分享标题:React.JS中JSX的原理与关键实现
    当前路径:http://www.hantingmc.com/qtweb/news1/15751.html

    网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

    猜你还喜欢下面的内容

    静态网站知识

    分类信息网