Install antd-mobile Global importnpm install antd-mobile --save Import css in import 'antd-mobile/dist/antd-mobile.css'; Using antd components in import React from 'react'; import { Button } from 'antd-mobile'; const index = () => { return ( <div> <Button type="primary">primary</Button> </div> ); } export default index; Import on demandnpm install babel-plugin-import -s Install plugins and override customize-cra configuration api documentation npm install react-app-rewired customize-cra -s Command method for changing "scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-app-rewired eject" }, Create a new const { override, fixBabelImports } = require('customize-cra'); module.exports = override( fixBabelImports('import', { libraryName: 'antd-mobile', style: 'css', }), ); Delete the css introduced before in Introducing postcss px to remnpm install lib-flexible postcss-px2rem-exclude --save Import import 'lib-flexible' Modify const { override, fixBabelImports, addPostcssPlugins, addWebpackAlias} = require('customize-cra'); const path = require("path"); module.exports = override( fixBabelImports('import', { libraryName: 'antd-mobile', style: 'css', }), addPostcssPlugins( [require("postcss-px2rem-exclude") ( { remUnit: 75, //Design size remPrecision: 2, //Convert only to two decimal places exclude: /node_modules/i //Plugins do not need to convert to rem } ) ] ), addWebpackAlias({ "@": path.resolve(__dirname, "src") }) ); If you need to use less npm install less less-loader -s If the project starts with an error, it is because npm install [email protected] -s pit! Need to pay attention to the order const { override, fixBabelImports, addPostcssPlugins, addWebpackAlias, addLessLoader } = require('customize-cra'); const path = require("path"); module.exports = override( fixBabelImports('import', { libraryName: 'antd-mobile', style: true, //Default is 'css' }), addLessLoader({ javascriptEnabled: true, modifyVars: { "@brand-primary": "#1DA57A" }, //Custom theme}), addPostcssPlugins( [require("postcss-px2rem-exclude") ( { remUnit: 75, //Design size remPrecision: 2, //Convert only to two decimal places exclude: /node_modules/i //Plugins do not need to convert to rem } ) ] ), addWebpackAlias({ "@": path.resolve(__dirname, "src") }) ); Supplement: Solve the problem that the postcss configuration px to rem conversion fails due to the introduction of antd-mobile in the react project Today I used antd-mobile and found that the postcss I configured before was invalid. To prevent the next pitfall, I will record the solution: rewrite postcss in the config-overrides.js file and add the following code npm i react-app-rewire-postcss postcss-px2rem-exclude -S const { override, fixBabelImports, addWebpackAlias, addDecoratorsLegacy, } = require("customize-cra"); const path = require("path"); const rewirePostcss = require("react-app-rewire-postcss"); module.exports = override( //Configure on-demand loading fixBabelImports("import", { libraryName: "antd-mobile", style: "css", }), //Configuration file aliasaddWebpackAlias({ "@": path.resolve(__dirname, "src"), "@scss": path.resolve(__dirname, "src/assets/scss"), "@images": path.resolve(__dirname, "src/assets/images"), "@views": path.resolve(__dirname, "src/views"), "@network": path.resolve(__dirname, "src/network"), "@store": path.resolve(__dirname, "src/store"), "@components": path.resolve(__dirname, "src/components"), }), addDecoratorsLegacy(), (config, env) => { // Rewrite postcss rewirePostcss(config, { plugins: () => [ require("postcss-flexbugs-fixes"), require("postcss-preset-env")({ autoprefixer: { flexbox: "no-2009", }, stage: 3, }), require("postcss-px2rem-exclude")({ // Design draft width/10 remUnit: 1080 / 10, exclude: /node-modules/i, }), ], }); return config; } ); The above is the details of using React to build a mobile terminal using antd-mobile+postcss. For more information about building a mobile terminal using React, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Docker deploys Macvlan to achieve cross-host network communication
>>: The difference between MySQL database stored procedures and transactions
1 Download and start Tomcat Go to the official we...
Table of contents Basic Configuration Entry file ...
1.Lock? 1.1 What is a lock? The real meaning of a...
User Groups In Linux, every user must belong to a...
Effect Preview Press the "Click to Preview&q...
This article shares the use of js and jQuery tech...
Without further ado, let’s get straight to the co...
1. Prerequisites JDK has been installed echo $PAT...
Clickhouse Introduction ClickHouse is a column-or...
Installation, configuration, startup, login and c...
The methods and concepts of private filters and g...
This article shares the specific code of JavaScri...
I recently made a file system and found that ther...
A set of MySQL libraries for testing. The previou...
Automatically discover disks Configuration Key Va...