SoFunction
Updated on 2025-03-03

React large-screen visual scaffolding tutorial example

Initialize using create-react-app

# Use the create-react-app typescript templateyarn create react-app my-big-screen --template typescript
# After initialization, enter the my-big-screen file directory and execute ityarn start 
# localhost:3000 you can see the react hello page

Introducing antd UI library

# Under the root directoryyarn add antd

Customize configuration using craco plugin

Install craco

yarn add @craco/craco

ReviseThe file is as follows

# original"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }
# After modification"scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "craco eject"
  }

Add in the root directorydocument

 = {}

Customize antd theme, use less as css preprocessor

Installcraco-antd

yarn add craco-antd

Modify the file

const CracoAntDesignPlugin = require('craco-antd');
 = {
  plugins: [
    {
      plugin: CracoAntDesignPlugin,
      options: {
        customizeTheme: {
          '@primary-color': '#1DA57A',
        },
      },
    },
  ],
};

craco-less. Simulate the scope function of style in vue component

craco-less dependency is already built into the @craco/craco dependency, no need to install it actively

Configuration File

const CracoAntDesignPlugin = require('craco-antd')
const CracoLessPlugin = require('craco-less')
const { loaderByName } = require('@craco/craco')
 = {
 plugins: [
     {
         plugin: CracoAntDesignPlugin,
         options: {
             customizeTheme: {
                 '@primary-color': '#1DA57A'
             }
         }
     },
     {
         plugin: CracoLessPlugin,
         options: {
             modifyLessRule(lessRule, context) {
                 // You have to exclude these file suffixes first,
                 // if you want to modify the less module's suffix
                  = /\.m\.less$/
                 return lessRule
             },
             modifyLessModuleRule(lessModuleRule, context) {
                 // Configure the file suffix
                  = /\.m\.less$/
                 // Configure the generated local ident name.
                 const cssLoader = (loaderByName('css-loader'))
                  = {
                     localIdentName: '[local]_[hash:base64:5]'
                 }
                 return lessModuleRule
             }
         }
     }
 ]
}

Configuration * ./src/ * File

/// <reference types="react-scripts" />
// Define the global variable and then you can access it by window.*interface Window {
  __REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any
  AMap: any
}
declare module "*." {
  const classes: { readonly [key: string]: string };
  export default classes;
}

Example of usage (similar to vue scope)

# File Namingapp {
    font-size: 40px;
    color: #f00;
}
# Introduce in componentsimport styles from './'
<div className={styles['app']}>hello</div>

Use two plug-ins for code format checksum automatic formatting

The above operation version records

{
  "name": "xxx",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@craco/craco": "^6.2.0",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@types/react-router-dom": "^5.1.8",
    "@typescript-eslint/eslint-plugin": "^4.30.0",
    "@typescript-eslint/parser": "^4.30.0",
    "antd": "^4.16.13",
    "craco-antd": "^1.19.0",
    "eslint": "^7.32.0",
    "eslint-config-alloy": "^4.3.0",
    "eslint-loader": "^4.0.2",
    "eslint-plugin-react": "^7.25.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.4",
    "react-router-dom": "^5.2.1",
    "react-scripts": "4.0.3",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "set PORT=3090 && craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "craco eject",
    "lint": "eslint '{src}/**/*.{ts,tsx,js,jsx}'",
    "lint_fix": "eslint {src} --ext .ts,.tsx,.js,jsx --fix"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

The above is the detailed content of the React large-screen visual scaffolding tutorial example. For more information about React large-screen visual scaffolding, please pay attention to my other related articles!