Skip to content

Commit

Permalink
Merge pull request ant-design#3066 from ant-design/antd-init@2
Browse files Browse the repository at this point in the history
Introduce dva with practical projects and play demo antd-init@2
  • Loading branch information
yiminghe authored Sep 19, 2016
2 parents d6b48ad + 681576d commit a131cab
Show file tree
Hide file tree
Showing 4 changed files with 548 additions and 6 deletions.
6 changes: 3 additions & 3 deletions docs/react/getting-started.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ A new project can be created using CLI tools.

```bash
$ mkdir antd-demo && cd antd-demo
$ antd-init --type plain-react
$ antd-init
```

`antd-init` will run `npm install` after a project is created. If it fails, you can run `npm install` by yourself.
Expand All @@ -55,7 +55,7 @@ $ antd-init --type plain-react
By default, besides the scaffolding needed to start the development, a fully working Todo application is created.
You may study this example later. For now, just follow this guide in order to get some experience working with the result of `antd-init`.

Replace the content of `src/entries/index.js` with the following code.
Replace the content of `index.js` with the following code.
As you can see, there is no difference between antd's components and usual React components.

```jsx
Expand Down Expand Up @@ -91,7 +91,7 @@ ReactDOM.render(<App />, document.getElementById('root'));
### 4. Development & Debugging

Run your project and visit http://127.0.0.1:8989
Run your project and visit http://127.0.0.1:8000

```bash
$ npm start
Expand Down
6 changes: 3 additions & 3 deletions docs/react/getting-started.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $ npm install antd-init -g

```bash
$ mkdir antd-demo && cd antd-demo
$ antd-init --type plain-react
$ antd-init
```

antd-init 会自动安装 npm 依赖,若有问题则可自行安装。
Expand All @@ -55,7 +55,7 @@ antd-init 会自动安装 npm 依赖,若有问题则可自行安装。

脚手架会生成一个 Todo 应用实例(一个很有参考价值的 React 上手示例),先不管它,我们用来测试组件。

直接用下面的代码替换 `src/entries/index.js` 的内容,用 React 的方式直接使用 antd 组件。
直接用下面的代码替换 `index.js` 的内容,用 React 的方式直接使用 antd 组件。

```jsx
import React from 'react';
Expand Down Expand Up @@ -90,7 +90,7 @@ ReactDOM.render(<App />, document.getElementById('root'));
### 4. 开发调试

一键启动调试,访问 http://127.0.0.1:8989 查看效果。
一键启动调试,访问 http://127.0.0.1:8000 查看效果。

```bash
$ npm start
Expand Down
271 changes: 271 additions & 0 deletions docs/react/practical-projects.en-US.md
Original file line number Diff line number Diff line change
@@ -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 (
<h2>List of Products</h2>
);
};

exports default Products;
```

Add routing infomation to router, edit `router.js`:

```diff
+ import Products from './routes/Products';
...
+ <Route path="/products" component={Products} />
```

Then open http://localhost:8989/#/products in your browser, you should be able to see the `<h2>` tag defined before.

## Write UI Components

As your application grows and you notice you are sharing UI elements between multiple pages (or using them multiple times on the same page), in dva it's called reusable components.

Let's create a `ProductList` component that we can use in multiple places to show a list of products.

Create `components/ProductList.js` and typing:

```javascript
import React, { PropTypes } from 'react';
import { Table, Popconfirm, Button } from 'antd';

const ProductList = ({ onDelete, products }) => {
const columns = [
{
title: 'Name',
dataIndex: 'name',
},
{
title: 'Actions',
render(text, record) {
return (
<Popconfirm title="Delete?" onConfirm={onDelete.bind(this, record.id)}>
<Button>删除</Button>
</Popconfirm>
);
},
},
];
return (
<Table
dataSource={products}
columns={columns}
/>
);
};

ProductList.proptypes = {
onDelete: PropTypes.func.isRequired,
products: PropTypes.array.isRequired,
};

export default ProductList;
```

## Define Model

After complete the UI, we will begin processing the data and logic.

dva manages domain model with `model`, with reducers for synchronous state update, effects for async logic, and subscriptions for data source subscribe.

Let's create a model `models/products.js` and typing:

```javascript
import dva from 'dva';

export default {
namespace: 'products',
state: [],
reducers: {
'delete'(state, { payload: id }) {
return state.filter(item => item.id !== id);
},
},
};
```

In this model:

- `namespace` represent the key on global state
- `state` is the initial value, here is an empty array
- `reducers` is equal to reducer in redux, accepting action, and update state synchronously

Then don't forget to require it in `index.js`:

```diff
// 3. Model
+ app.model(require('./models/products'));
```

## Connect

So far, wee have completed a seperate model and component. Then how to connect these together?

dva provides a `connect` method. If you are familar with redux, this `connect` is from react-router.

Edit `routes/Products.js` and replace with following:

```javascript
import React from 'react';
import { connect } from 'dva';
import ProductList from '../components/ProductList';

const Products = (props) => {

function handleDelete(id) {
props.dispatch({
type: 'products/delete',
payload: id,
});
}

return (
<div>
<h2>List of Products</h2>
<ProductList onDelete={handleDelete} products={props.products} />
</div>
);
};

// export default Products;
export default connect(({ products }) => ({
products
}))(Products);
```

Finally, we need some initial data to make the application run together. Edit `index.js`:

```diff
- const app = dva();
+ const app = dva({
+ initialState: {
+ products: [
+ { name: 'dva', id: 1 },
+ { name: 'antd', id: 2 },
+ ],
+ },
+ });
```

Refresh your browser, you should see the following result:

<p align="center">
<img src="https://zos.alipayobjects.com/rmsportal/GQJeDDeUCSTRMMg.gif" />
</p>

## 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)
Loading

0 comments on commit a131cab

Please sign in to comment.