npx create-rect-app 項目名稱 配置antd design mobile
npm install -- save antd- mobile
import { Button } from 'antd-mobile'
< Button> < / Button>
更改webpack配置
npm install craco -- save- dev
"scripts" : { "start" : "craco start" , "build" : "craco build" , "test" : "craco test" }
const path = require ( 'path' ) ; module. exports = { style : { postcss : { loaderOptions : { postcssOptions : { config : path. resolve ( __dirname, "postcss.config.js" ) } } } } , babel : { plugins : [ [ "import" , { libraryName : "antd-mobile" , libraryDirectory : "es/components" , style : false } ] ] } , webpack : { alias : { '@' : path. resolve ( __dirname, 'src/' ) , '@components' : path. resolve ( __dirname, 'src/components/' ) , '@assets' : path. resolve ( __dirname, 'src/assets/' ) } }
} ;
const pxToViewport = require ( 'postcss-px-to-viewport-8-plugin' ) ; module. exports = { plugins : [ pxToViewport ( { unitToConvert : 'px' , viewportWidth : 375 , unitPrecision : 5 , propList : [ '*' ] , viewportUnit : 'vw' , fontViewportUnit : 'vw' , selectorBlackList : [ ] , minPixelValue : 1 , mediaQuery : false , replace : true , exclude : [ / node_modules / , / antd-mobile / ] , include : / \.(css|scss)$ / } ) ]
} ;
配置路由(react-router-dom)
import React, { Suspense } from "react" ;
import { BrowserRouter, Routes, Route, Link } from "react-router-dom" ; const Home = React. lazy ( ( ) => import ( "@/pages/home" ) )
const Login = React. lazy ( ( ) => import ( "@/pages/login" ) ) export default function App ( ) { return ( < BrowserRouter> < Link to= "/login" > login< / Link> < Link to= "/home" > home< / Link> < Suspense fallback= { ( < div> loading... < / div> ) } > < Routes> < Route path= "/" element= { < Home / > } / > < Route path= "login" element= { < Login / > } / > < Route path= "home" element= { < Home / > } / > < / Routes> < / Suspense> < / BrowserRouter> ) ;
}
配置redux 參考文檔
npm install @reduxjs/ toolkit react- redux
import { configureStore } from '@reduxjs/toolkit'
import counterReducer from './modules/counterReducer'
export default configureStore ( { reducer : { counter:counterReducer }
} )
import { createSlice} from '@reduxjs/toolkit'
export const counterStore = createSlice ( { name : 'counter' , initialState : { count : 1 } , reducers : { updateData : ( state, action ) => { console. log ( 'state:action' , state, action) ; state. count = action. payload} }
} )
const fetchData = ( ) => { return async ( dispatch ) => { const res = await 請求方法 ( 請求地址) dispatch ( updateData ( res. data) ) }
} const { updateData } = counterStore. actions;
const counterReducer = counterStore. reducer; export { updateData, fetchData }
export default counterReducer;
import store from './store'
root. render ( < Provider store= { store} > < App / > < / Provider>
)
import { useSelector, useDispatch } from "react-redux" ;
import { updateData, fetchData } from "@/store/modules/counterReducer"
const { count} = useSelector ( ( state ) => state. counter) ;
const dispatch = useDispatch ( )
dispatch ( { type : 'counter/updateData' , payload : 8 } )
或
dispatch ( updateData ( 8 ) )
useEffect ( ( ) => { dispatch ( fetchData ( ) )
} , [ dispatch] )