项目实战

    是一个基于 Redux 的 轻量级数据流方案,概念来自 elm,支持 side effects、热替换、动态加载、react-native、SSR 等,已在生产环境广泛应用。

    umi 则是一个可插拔的企业级 react 应用框架。umi 以路由为基础的,支持,以及各种进阶的路由功能,并以此进行功能扩展,比如支持路由级的按需加载。然后配以完善的,覆盖从源码到构建产物的每个生命周期,支持各种功能扩展和业务需求。

    本文会引导你使用 umi、dva 和 antd 从 0 开始创建一个简单应用。

    先创建一个空目录,

    推荐使用 yarn 创建应用,执行以下命令,

    1. $ yarn create umi
    2. yarn create v1.12.0
    3. [1/4] 🔍 Resolving packages...
    4. [2/4] 🚚 Fetching packages...
    5. [3/4] 🔗 Linking dependencies...
    6. [4/4] 📃 Building fresh packages...
    7. success Installed "create-umi@0.9.5" with binaries:
    8. - create-umi

    yarn 会先安装最新版的 create-umi,然后提供交互式的提示来创建应用。

    选择 app, 然后回车确认。

    1. ? Select the boilerplate type
    2. ant-design-pro - Create project with an layout-only ant-design-pro boilerplate, use together with umi block.
    3. app - Create project with a simple boilerplate, support typescript.
    4. block - Create a umi block.
    5. library - Create a library with umi.
    6. plugin - Create a umi plugin.

    选上 antddva,然后回车确认。

    1. create package.json
    2. create mock/.gitkeep
    3. create src/assets/yay.jpg
    4. create src/layouts/index.css
    5. create src/layouts/index.js
    6. create src/pages/index.css
    7. create src/pages/index.js
    8. create src/global.css
    9. create .gitignore
    10. create .editorconfig
    11. create .env
    12. create .umirc.js
    13. create .eslintrc
    14. create .prettierrc
    15. create .prettierignore
    16. create src/models/.gitkeep
    17. File Generate Done
    18. Done in 966.73s.

    然后安装依赖,

    1. $ yarn

    然后启动应用,

    几秒钟后,你会看到以下输出,

    1. App running at:
    2. - Local: http://localhost:8000/
    3. - Network: http://{{ YourIP }}:8000/

    在浏览器里打开 ,你会看到 umi 的欢迎界面。

    使用 antd

    前面选择 antd 之后,会自动处理 antd 的依赖以及按需加载。你可以检查 .umirc.js 里的配置,确保 antd 已开启。

    1. // ref: https://umijs.org/config/
    2. export default {
    3. plugins: [
    4. // ref: https://umijs.org/plugin/umi-plugin-react.html
    5. [
    6. 'umi-plugin-react',
    7. {
    8. antd: true,
    9. dva: true,
    10. },
    11. ],
    12. ],
    13. };

    如果你没有 npx,需要先安装他,用于执行 node_modules 下的命令,

    1. $ yarn global add npx

    然后通过命令创建 /products 路由,

    1. $ npx umi g page products
    2. create src/pages/products.js
    3. create src/pages/products.css
    4. success

    然后在浏览器里打开 http://localhost:8000/products,你应该能看到对应的页面。

    编写 UI Component

    随着应用的发展,你会需要在多个页面分享 UI 元素 (或在一个页面使用多次),在 umi 里你可以把这部分抽成 component 。

    我们来编写一个 ProductList component,这样就能在不同的地方显示产品列表了。

    新建 src/components/ProductList.js 文件:

    完成 UI 后,现在开始处理数据和逻辑。

    dva 通过 model 的概念把一个领域的模型管理起来,包含同步更新 state 的 reducers,处理异步逻辑的 effects,订阅数据源的 subscriptions 。

    新建 model src/models/products.js

    1. export default {
    2. namespace: 'products',
    3. state: [],
    4. reducers: {
    5. delete(state, { payload: id }) {
    6. return state.filter(item => item.id !== id);
    7. },
    8. },
    9. };

    这个 model 里:

    • namespace 表示在全局 state 上的 key

    • 是初始值,在这里是空数组

    • reducers 等同于 redux 里的 reducer,接收 action,同步更新 state

    umi 里约定 src/models 下的 model 会被自动注入,你无需手动注入。

    connect 起来

    到这里,我们已经单独完成了 model 和 component,那么他们如何串联起来呢?

    dva 提供了 connect 方法。如果你熟悉 redux,这个 connect 来自 react-redux。

    编辑 src/pages/products.js,替换为以下内容:

    1. import { connect } from 'dva';
    2. const Products = ({ dispatch, products }) => {
    3. function handleDelete(id) {
    4. dispatch({
    5. type: 'products/delete',
    6. payload: id,
    7. });
    8. }
    9. return (
    10. <div>
    11. <h2>List of Products</h2>
    12. <ProductList onDelete={handleDelete} products={products} />
    13. </div>
    14. );
    15. };
    16. export default connect(({ products }) => ({
    17. products,
    18. }))(Products);
    1. export const dva = {
    2. config: {
    3. onError(err) {
    4. err.preventDefault();
    5. console.error(err.message);
    6. },
    7. initialState: {
    8. products: [{ name: 'dva', id: 1 }, { name: 'antd', id: 2 }],
    9. },
    10. },
    11. };

    刷新浏览器,应该能看到以下效果:

    项目实战 - 图1

    完成开发并且在开发环境验证之后,就需要部署给我们的用户了。先执行下面的命令,

    1. $ npm run build

    几秒后,输出应该如下:

    build 命令会打包所有的资源,包含 JavaScript, CSS, web fonts, images, html 等。你可以在 dist/ 目录下找到这些文件。

    下一步

    我们已经完成了一个简单应用,你可能还有很多疑问,比如:

    • 如何统一处理出错?

    • 如何处理更多路由,比如动态路由,嵌套路由,权限路由等?

    • 如何 mock 数据?

    • 如何部署?

    • 等等

    你可以: