2022-12-06 17:45:10 +08:00
|
|
|
|
---
|
|
|
|
|
category: Components
|
|
|
|
|
group: 其他
|
|
|
|
|
title: App
|
2024-03-22 14:22:42 +08:00
|
|
|
|
subtitle: 包裹组件
|
|
|
|
|
description: 提供重置样式和提供消费上下文的默认环境。
|
2022-12-14 21:49:41 +08:00
|
|
|
|
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*HJz8SZos2wgAAAAAAAAAAAAADrJ8AQ/original
|
2023-02-09 22:17:31 +08:00
|
|
|
|
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*oC92TK44Ex8AAAAAAAAAAAAADrJ8AQ/original
|
2022-12-06 17:45:10 +08:00
|
|
|
|
demo:
|
|
|
|
|
cols: 2
|
2023-08-08 19:48:41 +08:00
|
|
|
|
tag: New
|
2022-12-06 17:45:10 +08:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 何时使用
|
|
|
|
|
|
|
|
|
|
- 提供可消费 React context 的 `message.xxx`、`Modal.xxx`、`notification.xxx` 的静态方法,可以简化 useMessage 等方法需要手动植入 `contextHolder` 的问题。
|
|
|
|
|
- 提供基于 `.ant-app` 的默认重置样式,解决原生元素没有 antd 规范样式的问题。
|
|
|
|
|
|
|
|
|
|
## 代码演示
|
|
|
|
|
|
|
|
|
|
<!-- prettier-ignore -->
|
2023-01-31 09:53:11 +08:00
|
|
|
|
<code src="./demo/basic.tsx">基本用法</code>
|
2023-07-17 11:30:06 +08:00
|
|
|
|
<code src="./demo/config.tsx">Hooks 配置</code>
|
2022-12-06 17:45:10 +08:00
|
|
|
|
|
2022-12-14 21:49:41 +08:00
|
|
|
|
## 如何使用
|
|
|
|
|
|
|
|
|
|
### 基础用法
|
|
|
|
|
|
|
|
|
|
App 组件通过 `Context` 提供上下文方法调用,因而 useApp 需要作为子组件才能使用,我们推荐在应用中顶层包裹 App。
|
2022-12-06 17:45:10 +08:00
|
|
|
|
|
2022-12-07 18:27:02 +08:00
|
|
|
|
```tsx
|
2023-01-31 10:21:17 +08:00
|
|
|
|
import React from 'react';
|
2023-08-30 23:02:35 +08:00
|
|
|
|
import { App } from 'antd';
|
2022-12-07 18:27:02 +08:00
|
|
|
|
|
|
|
|
|
const MyPage: React.FC = () => {
|
2022-12-06 17:45:10 +08:00
|
|
|
|
const { message, notification, modal } = App.useApp();
|
|
|
|
|
message.success('Good!');
|
|
|
|
|
notification.info({ message: 'Good' });
|
|
|
|
|
modal.warning({ title: 'Good' });
|
|
|
|
|
// ....
|
2022-12-07 18:27:02 +08:00
|
|
|
|
// other message, notification, modal static function
|
2022-12-06 17:45:10 +08:00
|
|
|
|
return <div>Hello word</div>;
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-07 18:27:02 +08:00
|
|
|
|
const MyApp: React.FC = () => (
|
2022-12-06 17:45:10 +08:00
|
|
|
|
<App>
|
|
|
|
|
<MyPage />
|
|
|
|
|
</App>
|
|
|
|
|
);
|
2022-12-07 18:27:02 +08:00
|
|
|
|
|
|
|
|
|
export default MyApp;
|
2022-12-06 17:45:10 +08:00
|
|
|
|
```
|
2022-12-14 21:49:41 +08:00
|
|
|
|
|
|
|
|
|
注意:App.useApp 必须在 App 之下方可使用。
|
|
|
|
|
|
|
|
|
|
### 与 ConfigProvider 先后顺序
|
|
|
|
|
|
2022-12-15 16:03:18 +08:00
|
|
|
|
App 组件只能在 `ConfigProvider` 之下才能使用 Design Token, 如果需要使用其样式重置能力,则 ConfigProvider 与 App 组件必须成对出现。
|
2022-12-14 21:49:41 +08:00
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
<ConfigProvider theme={{ ... }}>
|
|
|
|
|
<App>
|
|
|
|
|
...
|
|
|
|
|
</App>
|
|
|
|
|
</ConfigProvider>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 内嵌使用场景(如无必要,尽量不做嵌套)
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
<App>
|
|
|
|
|
<Space>
|
|
|
|
|
...
|
|
|
|
|
<App>...</App>
|
|
|
|
|
</Space>
|
|
|
|
|
</App>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 全局场景(redux 场景)
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
// Entry component
|
|
|
|
|
import { App } from 'antd';
|
|
|
|
|
import type { MessageInstance } from 'antd/es/message/interface';
|
|
|
|
|
import type { ModalStaticFunctions } from 'antd/es/modal/confirm';
|
2023-01-31 10:21:17 +08:00
|
|
|
|
import type { NotificationInstance } from 'antd/es/notification/interface';
|
2022-12-14 21:49:41 +08:00
|
|
|
|
|
|
|
|
|
let message: MessageInstance;
|
|
|
|
|
let notification: NotificationInstance;
|
|
|
|
|
let modal: Omit<ModalStaticFunctions, 'warn'>;
|
|
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
|
const staticFunction = App.useApp();
|
|
|
|
|
message = staticFunction.message;
|
|
|
|
|
modal = staticFunction.modal;
|
|
|
|
|
notification = staticFunction.notification;
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { message, notification, modal };
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
// sub page
|
2023-01-31 10:21:17 +08:00
|
|
|
|
import React from 'react';
|
2023-08-30 23:02:35 +08:00
|
|
|
|
import { Button, Space } from 'antd';
|
|
|
|
|
|
2023-01-31 10:21:17 +08:00
|
|
|
|
import { message } from './store';
|
2022-12-14 21:49:41 +08:00
|
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
|
const showMessage = () => {
|
|
|
|
|
message.success('Success!');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Space>
|
|
|
|
|
<Button type="primary" onClick={showMessage}>
|
|
|
|
|
Open message
|
|
|
|
|
</Button>
|
|
|
|
|
</Space>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
```
|
2023-02-15 15:05:21 +08:00
|
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
|
2023-08-08 18:27:48 +08:00
|
|
|
|
通用属性参考:[通用属性](/docs/react/common-props)
|
|
|
|
|
|
2023-02-15 15:05:21 +08:00
|
|
|
|
### App
|
|
|
|
|
|
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| --- | --- | --- | --- | --- |
|
2023-10-12 11:08:01 +08:00
|
|
|
|
| component | 设置渲染元素,为 `false` 则不创建 DOM 节点 | ComponentType | div | 5.11.0 |
|
2023-02-15 15:05:21 +08:00
|
|
|
|
| message | App 内 Message 的全局配置 | [MessageConfig](/components/message-cn/#messageconfig) | - | 5.3.0 |
|
|
|
|
|
| notification | App 内 Notification 的全局配置 | [NotificationConfig](/components/notification-cn/#notificationconfig) | - | 5.3.0 |
|
2023-04-11 10:25:24 +08:00
|
|
|
|
|
2023-08-30 23:02:35 +08:00
|
|
|
|
## 主题变量(Design Token)
|
2023-04-11 10:25:24 +08:00
|
|
|
|
|
|
|
|
|
<ComponentTokenTable component="App"></ComponentTokenTable>
|