diff --git a/docs/react/practical-projects.en-US.md b/docs/react/practical-projects.en-US.md new file mode 100644 index 0000000000..fa2f55d000 --- /dev/null +++ b/docs/react/practical-projects.en-US.md @@ -0,0 +1,271 @@ +--- +order: 3 +title: Practical Projects +--- + +[dva](https://github.com/dvajs/dva) is a React and redux based, lightweight and elm-style framework, which supports side effects, hot module replacement, dynamic on demand, react-native, SSR. And it has been widely used in production environment. + +This article will guide you to create a simple application from zero using dva and antd. + +Include the following: + +--- + +## Install dva + +Install dva with npm. + +```bash +$ npm install dva-cli -g +``` + +## Create New App + +After installed dva-cli, you can have access to the `dva` command in terminal. Now, create a new application with `dva new`. + +```bash +$ dva new dva-quickstart +``` + +This creates `dva-quickstart` directory, that contains the project directories and files, and provides development server, build script, mock service, proxy server and so on. + +Then `cd` the `dva-quickstart` directory, and start the development server. + +```bash +$ cd dva-quickstart +$ npm start +``` + +After a few seconds, you will see thw following output: + +```bash + proxy: load rule from proxy.config.js + proxy: listened on 8989 +📦 411/411 build modules +webpack: bundle build is now finished. +``` + +Open http://localhost:8989 in your browser, you will see dva welcome page. + +## Integrate antd + +Install `antd` and `babel-plugin-antd` with npm. `babel-plugin-antd` is used to automatically import scripts and stylesheets from antd. See [repo](https://github.com/ant-design/babel-plugin-antd) 。 + +```bash +$ npm install antd babel-plugin-antd --save +``` + +Edit `webpack.config.js` to integrate `babel-plugin-antd`. + +```diff ++ webpackConfig.babel.plugins.push(['antd', { ++ style: 'css' ++ }]); +``` + +> Notice: No need to manually restart the server, it will restart automatically after you save the `webpack.config.js`. + +## Define Router + +We need to write an application displaying the list of products. The first step is to create a route. + +Create a route component `routes/Products.js`: + +```javascript +import React from 'react'; + +const Products = (props) => { + return ( +
+ +
+ +## Build + +Now that we've written our application and verified that it works in development, it's time to get it ready to deploy to our users. To do so, run the following command: + +```bash +$ npm run build +``` + +After a few seconds, the output should be as follows: + +```bash +Child + Time: 14008ms + Asset Size Chunks Chunk Names + index.html 255 bytes [emitted] + common.js 1.18 kB 0 [emitted] common + index.js 504 kB 1, 0 [emitted] index + index.css 127 kB 1, 0 [emitted] index +``` + +The `build` command packages up all of the assets that make up your application —— JavaScript, templates, CSS, web fonts, images, and more. Then you can find these files in the `dist /` directory. + +## What's Next + +We have completed a simple application, but you may still have lots of questions, such as: + +- How to dealing with async logic +- How to load initial data elegantly +- How to handle onError globally and locally +- How to load Routes and Models on demand +- How to implement HMR +- How to mock data +- and so on... + +You can: + +- Visit [dva offical website](https://github.com/dvajs/dva) +- View all the [API](https://github.com/dvajs/dva#api) +- View [toturial](https://github.com/dvajs/dva-docs/blob/master/v1/zh-cn/tutorial/01-%E6%A6%82%E8%A6%81.md), complete a medium application step by step +- View examples, such as [dva version of hackernews](https://github.com/dvajs/dva-hackernews) diff --git a/docs/react/practical-projects.zh-CN.md b/docs/react/practical-projects.zh-CN.md new file mode 100644 index 0000000000..42ba77055c --- /dev/null +++ b/docs/react/practical-projects.zh-CN.md @@ -0,0 +1,271 @@ +--- +order: 3 +title: 项目实战 +--- + +[dva](https://github.com/dvajs/dva) 是一个基于 react 和 redux 的轻量应用框架,概念来自 elm,支持 side effects、热替换、动态加载、react-native、SSR 等,已在生产环境广泛应用。 + +本文会引导你使用 dva 和 antd 从 0 开始创建一个简单应用。 + +会包含以下内容: + +--- + +## 安装 dva + +通过 npm 安装 dva 。 + +```bash +$ npm install dva-cli -g +``` + +## 创建新应用 + +安装完 dva-cli 之后,就可以在 terminal 里访问到 `dva` 命令。现在,你可以通过 `dva new` 创建新应用。 + +```bash +$ dva new dva-quickstart +``` + +这会创建 `dva-quickstart` 目录,包含项目初始化目录和文件,并提供开发服务器、构建脚本、数据 mock 服务、代理服务器等功能。 + +然后我们 `cd` 进入 `dva-quickstart` 目录,并启动开发服务器: + +```bash +$ cd dva-quickstart +$ npm start +``` + +几秒钟后,你会看到以下输出: + +```bash + proxy: load rule from proxy.config.js + proxy: listened on 8989 +📦 411/411 build modules +webpack: bundle build is now finished. +``` + +在浏览器里打开 http://localhost:8989 ,你会看到 dva 的欢迎界面。 + +## 使用 antd + +通过 npm 安装 `antd` 和 `babel-plugin-antd` 。`babel-plugin-antd` 是用来自动引入 antd 的脚本和样式的,详见 [repo](https://github.com/ant-design/babel-plugin-antd) 。 + +```bash +$ npm install antd babel-plugin-antd --save +``` + +编辑 `webpack.config.js`,使 `babel-plugin-antd` 插件生效。 + +```diff ++ webpackConfig.babel.plugins.push(['antd', { ++ style: 'css' ++ }]); +``` + +> 注:这里不需要手动重启开发服务器,保存 `webpack.config.js` 后会自动重启。 + +## 定义路由 + +我们要写个应用来先显示产品列表。首先第一步是创建路由,路由可以想象成是组成应用的不同页面。 + +新建 route component `routes/Products.js`,内容如下: + +```javascript +import React from 'react'; + +const Products = (props) => { + return ( ++ +
+ +## 构建应用 + +完成开发并且在开发环境验证之后,就需要部署给我们的用户了。先执行下面的命令: + +```bash +$ npm run build +``` + +几秒后,输出应该如下: + +```bash +Child + Time: 14008ms + Asset Size Chunks Chunk Names + index.html 255 bytes [emitted] + common.js 1.18 kB 0 [emitted] common + index.js 504 kB 1, 0 [emitted] index + index.css 127 kB 1, 0 [emitted] index +``` + +`build` 命令会打包所有的资源,包含 JavaScript, CSS, web fonts, images, html 等。然后你可以在 `dist/` 目录下找到这些文件。 + +## 下一步 + +我们已经完成了一个简单应用,你可能还有很多疑问,比如: + +- 如何处理异步请求 +- 如何优雅地加载初始数据 +- 如何统一处理出错,以及特定操作的出错 +- 如何动态加载路由和 Model,以加速页面载入速度 +- 如何实现 hmr +- 如何 mock 数据 +- 等等 + +你可以: + +- 访问 [dva 官网](https://github.com/dvajs/dva) +- 查看所有 [API](https://github.com/dvajs/dva#api) +- [教程](https://github.com/dvajs/dva-docs/blob/master/v1/zh-cn/tutorial/01-%E6%A6%82%E8%A6%81.md),一步步完成一个中型应用 +- 看看 [dva 版 hackernews](https://github.com/dvajs/dva-hackernews) 是[如何实现](https://github.com/sorrycc/blog/issues/9)的