When To Use
- When you need to show alert messages to users.
- When you need a persistent static container which is closable by user actions.
Examples
Basic
import React from 'react';
import { Alert } from 'antd';
const App: React.FC = () => <Alert message="Success Text" type="success" />;
export default App;
More types
import React from 'react';
import { Alert } from 'antd';
const App: React.FC = () => (
<>
<Alert message="Success Text" type="success" />
<br />
<Alert message="Info Text" type="info" />
<br />
<Alert message="Warning Text" type="warning" />
<br />
<Alert message="Error Text" type="error" />
</>
);
export default App;
Closable
import React from 'react';
import { CloseSquareFilled } from '@ant-design/icons';
import { Alert } from 'antd';
const onClose = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
console.log(e, 'I was closed.');
};
const App: React.FC = () => (
<>
<Alert
message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
type="warning"
closable
onClose={onClose}
/>
<br />
<Alert
message="Error Text"
description="Error Description Error Description Error Description Error Description Error Description Error Description"
type="error"
closable
onClose={onClose}
/>
<br />
<Alert
message="Error Text"
description="Error Description Error Description Error Description Error Description Error Description Error Description"
type="error"
closable={{
'aria-label': 'close',
closeIcon: <CloseSquareFilled />,
}}
onClose={onClose}
/>
</>
);
export default App;
Description
import React from 'react';
import { Alert } from 'antd';
const App: React.FC = () => (
<>
<Alert
message="Success Text"
description="Success Description Success Description Success Description"
type="success"
/>
<br />
<Alert
message="Info Text"
description="Info Description Info Description Info Description Info Description"
type="info"
/>
<br />
<Alert
message="Warning Text"
description="Warning Description Warning Description Warning Description Warning Description"
type="warning"
/>
<br />
<Alert
message="Error Text"
description="Error Description Error Description Error Description Error Description"
type="error"
/>
</>
);
export default App;
Icon
import React from 'react';
import { Alert } from 'antd';
const App: React.FC = () => (
<>
<Alert message="Success Tips" type="success" showIcon />
<br />
<Alert message="Informational Notes" type="info" showIcon />
<br />
<Alert message="Warning" type="warning" showIcon closable />
<br />
<Alert message="Error" type="error" showIcon />
<br />
<Alert
message="Success Tips"
description="Detailed description and advice about successful copywriting."
type="success"
showIcon
/>
<br />
<Alert
message="Informational Notes"
description="Additional description and information about copywriting."
type="info"
showIcon
/>
<br />
<Alert
message="Warning"
description="This is a warning notice about copywriting."
type="warning"
showIcon
closable
/>
<br />
<Alert
message="Error"
description="This is an error message about copywriting."
type="error"
showIcon
/>
</>
);
export default App;
Banner
import React from 'react';
import { Alert } from 'antd';
const App: React.FC = () => (
<>
<Alert message="Warning text" banner />
<br />
<Alert
message="Very long warning text warning text text text text text text text"
banner
closable
/>
<br />
<Alert showIcon={false} message="Warning text without icon" banner />
<br />
<Alert type="error" message="Error text" banner />
</>
);
export default App;
Loop Banner
import React from 'react';
import { Alert } from 'antd';
import Marquee from 'react-fast-marquee';
const App: React.FC = () => (
<Alert
banner
message={
<Marquee pauseOnHover gradient={false}>
I can be a React component, multiple React components, or just some text.
</Marquee>
}
/>
);
export default App;
Smoothly Unmount
import React, { useState } from 'react';
import { Alert, Switch } from 'antd';
const App: React.FC = () => {
const [visible, setVisible] = useState(true);
const handleClose = () => {
setVisible(false);
};
return (
<>
{visible && (
<Alert message="Alert Message Text" type="success" closable afterClose={handleClose} />
)}
<p>click the close button to see the effect</p>
<Switch onChange={setVisible} checked={visible} disabled={visible} />
</>
);
};
export default App;
ErrorBoundary
import React, { useState } from 'react';
import { Alert, Button } from 'antd';
const { ErrorBoundary } = Alert;
const ThrowError: React.FC = () => {
const [error, setError] = useState<Error>();
const onClick = () => {
setError(new Error('An Uncaught Error'));
};
if (error) {
throw error;
}
return (
<Button danger onClick={onClick}>
Click me to throw a error
</Button>
);
};
const App: React.FC = () => (
<ErrorBoundary>
<ThrowError />
</ErrorBoundary>
);
export default App;
Custom Icon
import React from 'react';
import { SmileOutlined } from '@ant-design/icons';
import { Alert } from 'antd';
const icon = <SmileOutlined />;
const App: React.FC = () => (
<>
<Alert icon={icon} message="showIcon = false" type="success" />
<br />
<Alert icon={icon} message="Success Tips" type="success" showIcon />
<br />
<Alert icon={icon} message="Informational Notes" type="info" showIcon />
<br />
<Alert icon={icon} message="Warning" type="warning" showIcon />
<br />
<Alert icon={icon} message="Error" type="error" showIcon />
<br />
<Alert
icon={icon}
message="Success Tips"
description="Detailed description and advice about successful copywriting."
type="success"
showIcon
/>
<br />
<Alert
icon={icon}
message="Informational Notes"
description="Additional description and information about copywriting."
type="info"
showIcon
/>
<br />
<Alert
icon={icon}
message="Warning"
description="This is a warning notice about copywriting."
type="warning"
showIcon
/>
<br />
<Alert
icon={icon}
message="Error"
description="This is an error message about copywriting."
type="error"
showIcon
/>
</>
);
export default App;
Custom action
import React from 'react';
import { Alert, Button, Space } from 'antd';
const App: React.FC = () => (
<>
<Alert
message="Success Tips"
type="success"
showIcon
action={
<Button size="small" type="text">
UNDO
</Button>
}
closable
/>
<br />
<Alert
message="Error Text"
showIcon
description="Error Description Error Description Error Description Error Description"
type="error"
action={
<Button size="small" danger>
Detail
</Button>
}
/>
<br />
<Alert
message="Warning Text"
type="warning"
action={
<Space>
<Button type="text" size="small">
Done
</Button>
</Space>
}
closable
/>
<br />
<Alert
message="Info Text"
description="Info Description Info Description Info Description Info Description"
type="info"
action={
<Space direction="vertical">
<Button size="small" type="primary">
Accept
</Button>
<Button size="small" danger ghost>
Decline
</Button>
</Space>
}
closable
/>
</>
);
export default App;
Component Token
import React from 'react';
import { SmileOutlined } from '@ant-design/icons';
import { Alert, ConfigProvider } from 'antd';
const icon = <SmileOutlined />;
const App: React.FC = () => (
<ConfigProvider
theme={{
components: {
Alert: {
withDescriptionIconSize: 32,
withDescriptionPadding: 16,
},
},
}}
>
<Alert
icon={icon}
message="Success Tips"
description="Detailed description and advice about successful copywriting."
type="success"
showIcon
/>
</ConfigProvider>
);
export default App;
API
Common props ref:Common props
Property |
Description |
Type |
Default |
Version |
action |
The action of Alert |
ReactNode |
- |
4.9.0 |
afterClose |
Called when close animation is finished |
() => void |
- |
|
banner |
Whether to show as banner |
boolean |
false |
|
closable |
The config of closable, >=5.15.0: support aria-* |
boolean | ({ closeIcon?: React.ReactNode } & React.AriaAttributes) |
false |
|
description |
Additional content of Alert |
ReactNode |
- |
|
icon |
Custom icon, effective when showIcon is true |
ReactNode |
- |
|
message |
Content of Alert |
ReactNode |
- |
|
showIcon |
Whether to show icon |
boolean |
false, in banner mode default is true |
|
type |
Type of Alert styles, options: success , info , warning , error |
string |
info , in banner mode default is warning |
|
onClose |
Callback when Alert is closed |
(e: MouseEvent) => void |
- |
|
Alert.ErrorBoundary
Property |
Description |
Type |
Default |
Version |
description |
Custom error description to show |
ReactNode |
{{ error stack }} |
|
message |
Custom error message to show |
ReactNode |
{{ error }} |
|
Design Token