New App Component which provide global style & static function replacement.
Static function in React 18 concurrent mode will not well support. In v5, we recommend to use hooks for the static replacement. But it will make user manual work on define this.
import React from 'react';
import { App, Button, Space } from 'antd';
// Sub page
const MyPage = () => {
const { message, modal, notification } = App.useApp();
const showMessage = () => {
message.success('Success!');
};
const showModal = () => {
modal.warning({
title: 'This is a warning message',
content: 'some messages...some messages...',
});
};
const showNotification = () => {
notification.info({
message: `Notification topLeft`,
description: 'Hello, Ant Design!!',
placement: 'topLeft',
});
};
return (
<Space>
<Button type="primary" onClick={showMessage}>
Open message
</Button>
<Button type="primary" onClick={showModal}>
Open modal
</Button>
<Button type="primary" onClick={showNotification}>
Open notification
</Button>
</Space>
);
};
// Entry component
export default () => (
<App>
<MyPage />
</App>
);
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.
import { App } from 'antd';import React from 'react';const MyPage: React.FC = () => {const { message, notification, modal } = App.useApp();message.success('Good!');notification.info({ message: 'Good' });modal.warning({ title: 'Good' });// ....// other message, notification, modal static functionreturn <div>Hello word</div>;};const MyApp: React.FC = () => (<App><MyPage /></App>);export default MyApp;
Note: App.useApp must be available under App.
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.
<ConfigProvider theme={{ ... }}><App>...</App></ConfigProvider>
<App><Space>...<App>...</App></Space></App>
// Entry componentimport { App } from 'antd';import type { MessageInstance } from 'antd/es/message/interface';import type { ModalStaticFunctions } from 'antd/es/modal/confirm';import type { NotificationInstance } from 'antd/es/notification/interface';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 };
// sub pageimport { Button, Space } from 'antd';import React from 'react';import { message } from './store';export default () => {const showMessage = () => {message.success('Success!');};return (<Space><Button type="primary" onClick={showMessage}>Open message</Button></Space>);};
Property | Description | Type | Default | Version |
---|---|---|---|---|
message | Global config for Message | MessageConfig | - | 5.3.0 |
notification | Global config for Notification | NotificationConfig | - | 5.3.0 |