mirror of
https://github.com/ant-design/ant-design.git
synced 2025-01-18 06:03:38 +08:00
docs: update site use-with-create-react-app (#42904)
* docs: update use-with-create-react-app * update
This commit is contained in:
parent
05b8436c05
commit
b8eef9d1b2
@ -1,34 +1,26 @@
|
||||
---
|
||||
order: 4
|
||||
title: Use in create-react-app
|
||||
title: Usage with create-react-app
|
||||
---
|
||||
|
||||
[create-react-app](https://github.com/facebookincubator/create-react-app) is one of the best React application development tools. We are going to use `antd` within it and modify the webpack config for some customized needs.
|
||||
|
||||
---
|
||||
[create-react-app](https://create-react-app.dev/) is one of the best React application development tools, This article will try to use `create-react-app` to create a project, and introduce antd.
|
||||
|
||||
## Install and Initialization
|
||||
|
||||
Before all start, you may need install [yarn](https://github.com/yarnpkg/yarn/).
|
||||
Before all start, you may need install [yarn](https://github.com/yarnpkg/yarn/) or [pnpm](https://pnpm.io/).
|
||||
|
||||
```bash
|
||||
$ yarn create react-app antd-demo
|
||||
|
||||
# or
|
||||
|
||||
$ npx create-react-app antd-demo
|
||||
```
|
||||
<InstallDependencies npm='$ npx create-react-app antd-demo' yarn='$ yarn create react-app antd-demo' pnpm='$ pnpm create react-app antd-demo'></InstallDependencies>
|
||||
|
||||
The tool will create and initialize environment and dependencies automatically, please try config your proxy setting or use another npm registry if any network errors happen during it.
|
||||
|
||||
Then we go inside `antd-demo` and start it.
|
||||
Then we go inside project and start it.
|
||||
|
||||
```bash
|
||||
$ cd antd-demo
|
||||
$ yarn start
|
||||
```
|
||||
|
||||
Open the browser at http://localhost:3000/. It renders a header saying "Welcome to React" on the page.
|
||||
Open the browser at http://localhost:3000/. It renders a header saying `Welcome to React` on the page.
|
||||
|
||||
## Import antd
|
||||
|
||||
@ -59,10 +51,8 @@ $ yarn add antd
|
||||
Modify `src/App.js`, import Button component from `antd`.
|
||||
|
||||
```jsx
|
||||
import React from 'react';
|
||||
import { Button } from 'antd';
|
||||
import 'antd/dist/reset.css';
|
||||
import './App.css';
|
||||
import React from 'react';
|
||||
|
||||
const App = () => (
|
||||
<div className="App">
|
||||
@ -76,77 +66,3 @@ export default App;
|
||||
OK, you should now see a blue primary button displayed on the page. Next you can choose any components of `antd` to develop your application. Visit other workflows of `create-react-app` at its [User Guide](https://create-react-app.dev/docs/getting-started).
|
||||
|
||||
We are successfully running antd components now, go build your own application!
|
||||
|
||||
## Test with Jest
|
||||
|
||||
`create-react-app` comes with `jest` built in. Jest does not support `esm` modules, and Ant Design uses them. In order to test your Ant Design application with Jest you have to add the following to your `package.json` :
|
||||
|
||||
```json
|
||||
"jest": {
|
||||
"transformIgnorePatterns": [
|
||||
"/node_modules/(?!antd|@ant-design|rc-.+?|@babel/runtime).+(js|jsx)$"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Advanced Guides
|
||||
|
||||
In the real world, we usually have to modify default webpack config for custom needs such as themes. We can achieve that by using [craco](https://github.com/gsoft-inc/craco) which is one of create-react-app's custom config solutions.
|
||||
|
||||
Install craco and modify the `scripts` field in `package.json`.
|
||||
|
||||
```bash
|
||||
$ yarn add @craco/craco
|
||||
```
|
||||
|
||||
```diff
|
||||
/* package.json */
|
||||
"scripts": {
|
||||
- "start": "react-scripts start",
|
||||
- "build": "react-scripts build",
|
||||
- "test": "react-scripts test",
|
||||
+ "start": "craco start",
|
||||
+ "build": "craco build",
|
||||
+ "test": "craco test",
|
||||
}
|
||||
```
|
||||
|
||||
Then create a `craco.config.js` at root directory of your project for further overriding.
|
||||
|
||||
```js
|
||||
/* craco.config.js */
|
||||
module.exports = {
|
||||
// ...
|
||||
};
|
||||
```
|
||||
|
||||
### Customize Theme
|
||||
|
||||
Ref to the [Customize Theme documentation](/docs/react/customize-theme). Modify theme with ConfigProvider:
|
||||
|
||||
```tsx
|
||||
import React from 'react';
|
||||
import { ConfigProvider } from 'antd';
|
||||
|
||||
export default () => (
|
||||
<ConfigProvider
|
||||
theme={{
|
||||
token: {
|
||||
colorPrimary: '#00b96b',
|
||||
},
|
||||
}}
|
||||
>
|
||||
<MyApp />
|
||||
</ConfigProvider>
|
||||
);
|
||||
```
|
||||
|
||||
## eject
|
||||
|
||||
You can also eject your application using [yarn run eject](https://facebook.github.io/create-react-app/docs/available-scripts#npm-run-eject) for a custom setup of create-react-app, although you should dig into it by yourself.
|
||||
|
||||
## Summary
|
||||
|
||||
Finally, we used antd with create-react-app successfully, the source code of this guide was pushed to [create-react-app-antd](https://github.com/ant-design/create-react-app-antd) which you could clone and use it directly.
|
||||
|
||||
Next part, We will introduce how to use antd in [TypeScript](/docs/react/use-in-typescript) and [Umi](/docs/react/practical-projects), let's keep moving!
|
||||
|
@ -3,21 +3,13 @@ order: 4
|
||||
title: 在 create-react-app 中使用
|
||||
---
|
||||
|
||||
[create-react-app](https://github.com/facebookincubator/create-react-app) 是业界最优秀的 React 应用开发工具之一,本文会尝试在 create-react-app 创建的工程中使用 antd 组件,并自定义 webpack 的配置以满足各类工程化需求。
|
||||
|
||||
---
|
||||
[create-react-app](https://create-react-app.dev/) 是业界最优秀的 React 应用开发工具之一,本文会尝试使用 `create-react-app` 创建一个项目,并引入 antd。
|
||||
|
||||
## 安装和初始化
|
||||
|
||||
在开始之前,你可能需要安装 [yarn](https://github.com/yarnpkg/yarn/)。
|
||||
在开始之前,你可能需要安装 [yarn](https://github.com/yarnpkg/yarn/) 或者 [pnpm](https://pnpm.io/zh/)。
|
||||
|
||||
```bash
|
||||
$ yarn create react-app antd-demo
|
||||
|
||||
# or
|
||||
|
||||
$ npx create-react-app antd-demo
|
||||
```
|
||||
<InstallDependencies npm='$ npx create-react-app antd-demo' yarn='$ yarn create react-app antd-demo' pnpm='$ pnpm create react-app antd-demo'></InstallDependencies>
|
||||
|
||||
工具会自动初始化一个脚手架并安装 React 项目的各种必要依赖,如果在过程中出现网络问题,请尝试配置代理或使用其他 npm registry。
|
||||
|
||||
@ -59,10 +51,8 @@ $ yarn add antd
|
||||
修改 `src/App.js`,引入 antd 的按钮组件。
|
||||
|
||||
```jsx
|
||||
import React from 'react';
|
||||
import { Button } from 'antd';
|
||||
import 'antd/dist/reset.css';
|
||||
import './App.css';
|
||||
import React from 'react';
|
||||
|
||||
const App = () => (
|
||||
<div className="App">
|
||||
@ -76,69 +66,3 @@ export default App;
|
||||
好了,现在你应该能看到页面上已经有了 antd 的蓝色按钮组件,接下来就可以继续选用其他组件开发应用了。其他开发流程你可以参考 create-react-app 的[官方文档](https://create-react-app.dev/docs/getting-started)。
|
||||
|
||||
我们现在已经把 antd 组件成功运行起来了,开始开发你的应用吧!
|
||||
|
||||
## 高级配置
|
||||
|
||||
这个例子在实际开发中还有一些优化的空间,比如无法进行主题配置。
|
||||
|
||||
此时我们需要对 create-react-app 的默认配置进行自定义,这里我们使用 [craco](https://github.com/gsoft-inc/craco) (一个对 create-react-app 进行自定义配置的社区解决方案)。
|
||||
|
||||
现在我们安装 craco 并修改 `package.json` 里的 `scripts` 属性。
|
||||
|
||||
```bash
|
||||
$ yarn add @craco/craco
|
||||
```
|
||||
|
||||
```diff
|
||||
/* package.json */
|
||||
"scripts": {
|
||||
- "start": "react-scripts start",
|
||||
- "build": "react-scripts build",
|
||||
- "test": "react-scripts test",
|
||||
+ "start": "craco start",
|
||||
+ "build": "craco build",
|
||||
+ "test": "craco test",
|
||||
}
|
||||
```
|
||||
|
||||
然后在项目根目录创建一个 `craco.config.js` 用于修改默认配置。
|
||||
|
||||
```js
|
||||
/* craco.config.js */
|
||||
module.exports = {
|
||||
// ...
|
||||
};
|
||||
```
|
||||
|
||||
### 自定义主题
|
||||
|
||||
参考 [配置主题](/docs/react/customize-theme),通过 ConfigProvider 进行主题配置:
|
||||
|
||||
```tsx
|
||||
import React from 'react';
|
||||
import { ConfigProvider } from 'antd';
|
||||
|
||||
export default () => (
|
||||
<ConfigProvider
|
||||
theme={{
|
||||
token: {
|
||||
colorPrimary: '#00b96b',
|
||||
},
|
||||
}}
|
||||
>
|
||||
<MyApp />
|
||||
</ConfigProvider>
|
||||
);
|
||||
```
|
||||
|
||||
## eject
|
||||
|
||||
你也可以使用 create-react-app 提供的 [yarn run eject](https://create-react-app.dev/docs/available-scripts/#npm-run-eject) 命令将所有内建的配置暴露出来。不过这种配置方式需要你自行探索,不在本文讨论范围内。
|
||||
|
||||
## 小结
|
||||
|
||||
以上是在 create-react-app 中使用 antd 的相关实践,你也可以借鉴此文的做法在自己的 webpack 工作流中使用 antd。
|
||||
|
||||
上述教程的脚手架源码我们放在 [create-react-app-antd](https://github.com/ant-design/create-react-app-antd) 中,你可以直接下载使用。
|
||||
|
||||
接下来我们会介绍如何在 [TypeScript](/docs/react/use-in-typescript) 和 [Umi](/docs/react/practical-projects) 中使用 antd,欢迎继续阅读。
|
||||
|
Loading…
Reference in New Issue
Block a user