Home Web Front-end JS Tutorial How to implement on-demand loading in react

How to implement on-demand loading in react

Dec 20, 2022 am 10:30 AM
react

How to implement on-demand loading in react: 1. Accurately load components through "import 'antd/lib/button/style'"; 2. Implement on-demand loading by cooperating with the "babel-plugin-import" plug-in; 3. Use "babel-plugin-import react-app-rewired" to load on demand.

How to implement on-demand loading in react

#The operating environment of this tutorial: Windows 10 system, react18 version, Dell G3 computer.

How to implement on-demand loading in react?

3 ways to implement on-demand loading in react

1. Accurately load components

import Button from 'antd/lib/button'
import 'antd/lib/button/style'
Copy after login

2. Expose configuration, cooperate with babel- The plugin-import plug-in implements on-demand loading

babel-plugin-import is a babel plug-in for on-demand loading of components and styles

Exposed configuration

npm run eject
Copy after login

Installation plug-in

 npm install babel-plugin-import -S
Copy after login

Modify package.json

"babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
      [
        "import",
        {
          "libraryName": "antd",
          "libraryDirectory": "es",
          "style":"css"
        }
      ]
    ]
  }
Copy after login

Introduce directly after configuration: import {Button} from 'antd'

3. Via babel-plugin-import react-app-rewired Implement on-demand loading

react-app-rewired extends webpack configuration without exposed configuration

  //安装插件:
   npm install babel-plugin-import -S
  //修改(添加)config-overrides.js文件
  //引入react-app-rewired添加babel插件的函数
  const {injetBabelPlugin}=require('react-app-rewired')
  module.exports=function override(config,env){
 config=injetBabelPlugin([
    [
        "import",
        {
          "libraryName": "antd",
          "libraryDirectory": "es",
          "style":"css"
        }
      ]
 ],config);
 return config
 }:
Copy after login

Introduce directly after configuration: import {Button} from 'antd'

Recommended learning: "react video tutorial"

The above is the detailed content of How to implement on-demand loading in react. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build a real-time chat app with React and WebSocket How to build a real-time chat app with React and WebSocket Sep 26, 2023 pm 07:46 PM

How to build a real-time chat app with React and WebSocket

Guide to React front-end and back-end separation: How to achieve decoupling and independent deployment of front-end and back-end Guide to React front-end and back-end separation: How to achieve decoupling and independent deployment of front-end and back-end Sep 28, 2023 am 10:48 AM

Guide to React front-end and back-end separation: How to achieve decoupling and independent deployment of front-end and back-end

How to build simple and easy-to-use web applications with React and Flask How to build simple and easy-to-use web applications with React and Flask Sep 27, 2023 am 11:09 AM

How to build simple and easy-to-use web applications with React and Flask

How to build a reliable messaging app with React and RabbitMQ How to build a reliable messaging app with React and RabbitMQ Sep 28, 2023 pm 08:24 PM

How to build a reliable messaging app with React and RabbitMQ

React code debugging guide: How to quickly locate and solve front-end bugs React code debugging guide: How to quickly locate and solve front-end bugs Sep 26, 2023 pm 02:25 PM

React code debugging guide: How to quickly locate and solve front-end bugs

How to build a fast data analysis application using React and Google BigQuery How to build a fast data analysis application using React and Google BigQuery Sep 26, 2023 pm 06:12 PM

How to build a fast data analysis application using React and Google BigQuery

React Router User Guide: How to implement front-end routing control React Router User Guide: How to implement front-end routing control Sep 29, 2023 pm 05:45 PM

React Router User Guide: How to implement front-end routing control

React responsive design guide: How to achieve adaptive front-end layout effects React responsive design guide: How to achieve adaptive front-end layout effects Sep 26, 2023 am 11:34 AM

React responsive design guide: How to achieve adaptive front-end layout effects

See all articles