2022-12-06 17:45:10 +08:00
---
category: Components
group: Other
title: App
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-06-15 18:17:36 +08:00
Application wrapper for some global usages.
2022-12-06 17:45:10 +08:00
## When To Use
2023-06-15 18:17:36 +08:00
- Provide reset styles based on `.ant-app` element.
2023-06-28 11:37:14 +08:00
- You could use static methods of `message/notification/Modal` form `useApp` without writing `contextHolder` manually.
2022-12-06 17:45:10 +08:00
## Examples
<!-- prettier - ignore -->
2022-12-14 21:49:41 +08:00
< code src = "./demo/basic.tsx" > basic< / code >
2022-12-06 17:45:10 +08:00
## How to use
2022-12-14 21:49:41 +08:00
### Basic usage
App provides upstream and downstream method calls through `Context` , because useApp needs to be used as a subcomponent, we recommend encapsulating App at the top level in the application.
2022-12-07 18:27:02 +08:00
```tsx
2022-12-06 17:45:10 +08:00
import { App } from 'antd';
2023-02-15 10:21:28 +08:00
import React from 'react';
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
Note: App.useApp must be available under App.
2022-12-15 11:40:25 +08:00
### Sequence with ConfigProvider
2022-12-14 21:49:41 +08:00
The App component can only use the token in the `ConfigProvider` , if you need to use the Token, the ConfigProvider and the App component must appear in pairs.
```tsx
< ConfigProvider theme = {{ . . . } } >
< App >
...
< / App >
< / ConfigProvider >
```
### Embedded usage scenarios (if not necessary, try not to do nesting)
```tsx
< App >
< Space >
...
< App > ...< / App >
< / Space >
< / App >
```
### Global scene (redux scene)
```tsx
// Entry component
import { App } from 'antd';
import type { MessageInstance } from 'antd/es/message/interface';
import type { ModalStaticFunctions } from 'antd/es/modal/confirm';
2023-02-15 10:21:28 +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;
};
2023-07-04 11:41:25 +08:00
export { message, modal, notification };
2022-12-14 21:49:41 +08:00
```
```tsx
// sub page
import { Button, Space } from 'antd';
2023-02-15 10:21:28 +08:00
import React from 'react';
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
### App
| Property | Description | Type | Default | Version |
| --- | --- | --- | --- | --- |
| message | Global config for Message | [MessageConfig ](/components/message/#messageconfig ) | - | 5.3.0 |
| notification | Global config for Notification | [NotificationConfig ](/components/notification/#notificationconfig ) | - | 5.3.0 |
2023-04-11 10:25:24 +08:00
## Design Token
< ComponentTokenTable component = "App" > < / ComponentTokenTable >