How to implement on-demand loading in react

藏色散人
Release: 2022-12-20 10:30:49
Original
3036 people have browsed it

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!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!