Browse Source
* docs: update docs * Update use-with-umi.en-US.md * Update use-with-umi.zh-CN.mdpull/43027/head
lijianan
1 year ago
committed by
GitHub
6 changed files with 67 additions and 171 deletions
@ -1,75 +0,0 @@ |
|||
--- |
|||
order: 5 |
|||
title: Usage with TypeScript |
|||
--- |
|||
|
|||
Let's create a `TypeScript` project by using `create-react-app`, then import `antd` step by step. |
|||
|
|||
> We build `antd` based on latest stable version of TypeScript (`>=5.0.0`), please make sure your project dependency matches it. |
|||
|
|||
## Install and Initialization |
|||
|
|||
Ensure your system has installed latest version of [yarn](https://yarnpkg.com) or [npm](https://www.npmjs.com/). |
|||
|
|||
Create a new [cra-template-typescript](https://github.com/facebook/create-react-app/tree/main/packages/cra-template-typescript) project named `antd-demo-ts`. |
|||
|
|||
<InstallDependencies npm='$ npx create-react-app antd-demo-ts --template typescript' yarn='$ yarn create react-app antd-demo-ts --template typescript' pnpm='$ pnpm create react-app antd-demo-ts --template typescript'></InstallDependencies> |
|||
|
|||
If you are using `vite`, there will be an option during build where you can choose the `typescript` template. |
|||
|
|||
![](https://github-production-user-asset-6210df.s3.amazonaws.com/49217418/245092600-9af66ed4-f44d-4793-9d7a-179ff39fc284.png) |
|||
|
|||
Then we go inside `antd-demo-ts` and start it. |
|||
|
|||
```bash |
|||
$ cd antd-demo-ts |
|||
$ yarn start |
|||
``` |
|||
|
|||
Open browser at http://localhost:3000/, it renders a header saying "Welcome to React" on the page. |
|||
|
|||
## Import antd |
|||
|
|||
```bash |
|||
$ yarn add antd |
|||
``` |
|||
|
|||
Modify `src/App.tsx`, import Button component from `antd`. |
|||
|
|||
```tsx |
|||
import { Button } from 'antd'; |
|||
import React from 'react'; |
|||
|
|||
const App: React.FC = () => ( |
|||
<div className="App"> |
|||
<Button type="primary">Button</Button> |
|||
</div> |
|||
); |
|||
|
|||
export default App; |
|||
``` |
|||
|
|||
OK, reboot with `yarn start`, 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 it's [User Guide](https://create-react-app.dev/docs/getting-started#creating-a-typescript-app). |
|||
|
|||
`antd` is written in TypeScript with complete definitions, try out and enjoy the property suggestion and typing check. |
|||
|
|||
![](https://gw.alipayobjects.com/zos/antfincdn/26L5vPoLug/8d7da796-175e-40af-8eea-e7031ba09f9f.png) |
|||
|
|||
> Don't install `@types/antd`. |
|||
|
|||
### Customize Theme |
|||
|
|||
Ref to the [Customize Theme documentation](/docs/react/customize-theme). Modify theme with ConfigProvider: |
|||
|
|||
```tsx |
|||
import { ConfigProvider } from 'antd'; |
|||
import React from 'react'; |
|||
|
|||
const App: React.FC = () => ( |
|||
<ConfigProvider theme={{ token: { colorPrimary: '#00b96b' } }}> |
|||
<MyApp /> |
|||
</ConfigProvider> |
|||
); |
|||
|
|||
export default App; |
|||
``` |
@ -1,75 +0,0 @@ |
|||
--- |
|||
order: 5 |
|||
title: 在 TypeScript 中使用 |
|||
--- |
|||
|
|||
使用 `create-react-app` 一步步地创建一个 `TypeScript` 项目,并引入 antd。 |
|||
|
|||
> `antd` 基于最新稳定版本的 TypeScript(`>=5.0.0`),请确保项目中使用匹配的版本。 |
|||
|
|||
## 安装和初始化 |
|||
|
|||
请确保电脑上已经安装了最新版的 [yarn](https://yarnpkg.com) 或者 [npm](https://www.npmjs.com/)。 |
|||
|
|||
创建 [cra-template-typescript](https://github.com/facebook/create-react-app/tree/main/packages/cra-template-typescript) 项目。 |
|||
|
|||
<InstallDependencies npm='$ npx create-react-app antd-demo-ts --template typescript' yarn='$ yarn create react-app antd-demo-ts --template typescript' pnpm='$ pnpm create react-app antd-demo-ts --template typescript'></InstallDependencies> |
|||
|
|||
如果你使用的是 `vite` 作为脚手架,那么在创建过程中会有一个选项,你可以选择 `typescript` 模板。 |
|||
|
|||
![](https://github-production-user-asset-6210df.s3.amazonaws.com/49217418/245092600-9af66ed4-f44d-4793-9d7a-179ff39fc284.png) |
|||
|
|||
然后我们进入项目并启动。 |
|||
|
|||
```bash |
|||
$ cd antd-demo-ts |
|||
$ yarn start |
|||
``` |
|||
|
|||
此时浏览器会访问 http://localhost:3000/ ,看到 `Welcome to React` 的界面就算成功了。 |
|||
|
|||
## 引入 antd |
|||
|
|||
```bash |
|||
$ yarn add antd |
|||
``` |
|||
|
|||
修改 `src/App.tsx`,引入 antd 的按钮组件。 |
|||
|
|||
```tsx |
|||
import { Button } from 'antd'; |
|||
import React from 'react'; |
|||
|
|||
const App: React.FC = () => ( |
|||
<div className="App"> |
|||
<Button type="primary">Button</Button> |
|||
</div> |
|||
); |
|||
|
|||
export default App; |
|||
``` |
|||
|
|||
重新启动 `yarn start`,现在你应该能看到页面上已经有了 antd 的蓝色按钮组件,接下来就可以继续选用其他组件开发应用了。其他开发流程你可以参考 create-react-app 的[官方文档](https://create-react-app.dev/docs/getting-started#creating-a-typescript-app)。 |
|||
|
|||
`antd` 使用 TypeScript 书写并提供了完整的定义,你可以享受组件属性输入建议和定义检查的功能。 |
|||
|
|||
![](https://gw.alipayobjects.com/zos/antfincdn/26L5vPoLug/8d7da796-175e-40af-8eea-e7031ba09f9f.png) |
|||
|
|||
> 注意不要安装 `@types/antd`。 |
|||
|
|||
### 自定义主题 |
|||
|
|||
参考 [配置主题](/docs/react/customize-theme),通过 ConfigProvider 进行主题配置: |
|||
|
|||
```tsx |
|||
import { ConfigProvider } from 'antd'; |
|||
import React from 'react'; |
|||
|
|||
const App: React.FC = () => ( |
|||
<ConfigProvider theme={{ token: { colorPrimary: '#00b96b' } }}> |
|||
<MyApp /> |
|||
</ConfigProvider> |
|||
); |
|||
|
|||
export default App; |
|||
``` |
@ -1,6 +1,6 @@ |
|||
--- |
|||
order: 3 |
|||
title: 项目实战 |
|||
title: 在 Umi 中使用 |
|||
--- |
|||
|
|||
在真实项目开发中,除了 Ant Design 这样的 UI 库,你可能会还会需要构建工具、路由方案、CSS 方案、数据流方案、请求库和请求方案、国际化方案、权限方案、Icons 方案,等等,才能完成一个完整的项目。我们基于业务场景的场景,推出了基于 React 的企业级应用框架 [Umi](https://umijs.org/),推荐你在项目中使用。 |
Loading…
Reference in new issue