基于React、Ant-Design、Mobx和JSS的React应用
项目由Create React App启动
生产环境部署,服务器根目录指向build/下
# 安装依赖包
npm install 或 yarn(推荐)
# 本地运行
npm run dev 或 yarn start
# 生产环境
npm run build 或 yarn build
- 新建项目
react-study
create-react-app react-study
- 配置使用装饰器(路由、mobx、jss都需要)
- 暴露项目的配置项(保证已经git commit)
npm run eject
- 安装
babel
插件yarn add @babel/plugin-proposal-decorators @babel/plugin-transform-react-jsx @babel/plugin-transform-react-jsx-self @babel/plugin-transform-react-jsx-source --dev
- 修改
package.json
文件的babel
配置项"babel": { "plugins": [ ["@babel/plugin-proposal-decorators", { "legacy": true }] ], "presets": [ "react-app" ] }
- 暴露项目的配置项(保证已经git commit)
- 配置
webpack
,使用@
来import
模块// webpack.config.js alias: { '@': path.resolve(__dirname, '../src'), ... }
- CSS Reset
src/index.css
: 标签重置src/App.css
: 自定义样式、覆盖antd样式 - React路由: react-router-dom
- 安装
react-router-dom
yarn add react-router-dom
- router/config.js: 路由配置表
- router/index.js:路由处理
- 路由重定向(404,默认路由)、路由渲染、菜单高亮展开
React(Suspense/Lazy)
实现路由懒加载
- 安装
- UI组件库:ant-design
- 安装
yarn add antd
- 按需加载,只需从
antd
引入模块,不用单独引入样式package.json添加配置yarn add babel-plugin-import
"babel": { "plugins": [ [ "import", { "libraryName": "antd", "libraryDirectory": "es", "style": "css" } ] ] }
- 安装
- CSS-in-JS: JSS
- 安装
jss
yarn add react-jss
- 公共部分提取到文件夹
styles/
下,组件内部通过object spread
操作符或者Object.assign
获取subRoutes: { ...style.ul, paddingLeft: 10 }
- 组件内部通过
injectSheet
来绑定import injectSheet from 'react-jss'; import styles from './style'; @injectSheet(styles) class Mainbox extends Component { render() { const { classes } = this.props; return <div className={classes.mainContainer}> <Router /> </div> } }
- 安装
- react的状态存储(store): Mobx
- 安装
yarn add mobx mobx-react
- 全局挂载store
import { Provider } from 'mobx-react' function App() { return ( <Provider {...store}> <BrowserRouter> <Router /> </BrowserRouter> </Provider> ) }
- 安装
- 图表库:bizcharts
- 安装
yarn add bizcharts
- 安装
- config: 存放webpack的配置参数
- api: 根据模块来存放api的接口地址
- assets: 存储静态文件,如图片、字体、svg图标等
- components: 公共组件
- router: 路由的配置和挂载渲染
- store: 数据状态存储
- styles: 公共样式
- utils: 工具类函数
- token.js: 对token的读写操作
- math.js: 数学类的工具函数
- request.js:异步请求fetch的封装
- views: 页面级别的组件