Notification 通知提醒框
全局展示通知提醒信息。
何时使用#
在系统右上角显示通知提醒信息。经常用于以下情况:
- 较为复杂的通知内容。
- 带有交互的通知,给出用户下一步的行动点。
- 系统主动推送。
全局展示通知提醒信息。
在系统右上角显示通知提醒信息。经常用于以下情况:
import { Button, notification } from 'antd';
const openNotification = function() {
notification.open({
message: '这是标题',
description: '这是提示框的文案这是提示框的文案这是提示框的文案这是提示框的文案这是提示框的文案这是提示框的文案这是提示框的文案'
});
};
ReactDOM.render(
<Button type="primary" onClick={openNotification}>打开通知提醒框</Button>
, mountNode);
import { Button, notification } from 'antd';
const openNotificationWithIcon = function(type) {
return function(){
notification[type]({
message: '这是标题',
description: '这是提示框的文案这是提示框示框的文案这是提示是提示框的文案这是提示框的文案'
});
};
};
ReactDOM.render(<div>
<Button onClick={openNotificationWithIcon('success')}>成功</Button>
<Button onClick={openNotificationWithIcon('info')}>消息</Button>
<Button onClick={openNotificationWithIcon('warn')}>警告</Button>
<Button onClick={openNotificationWithIcon('error')}>错误</Button>
</div>
, mountNode);
import { Button, notification } from 'antd';
const close = function() {
console.log('我被默认的关闭按钮关闭了!');
};
const openNotification = function() {
const key = 'open' + Date.now();
const btnClick = function() {
// 隐藏提醒框
notification.close(key);
};
const btn = <Button type="primary" size="small" onClick={btnClick}>
自定义关闭按钮并触发回调函数
</Button>;
notification.open({
message: '这是标题',
description: '这是提示框的文案这是提示框示框的文案这是提示是提示框的文案这是提示框的文案',
btn: btn,
key: key,
onClose: close
});
};
ReactDOM.render(
<div>
<Button type="primary" onClick={openNotification}>打开通知提醒框</Button>
</div>,
mountNode);
import { Button, notification } from 'antd';
const openNotification = function() {
const args = {
message: '这是标题',
description: '我不会自动关闭,我不会自动关闭,我不会自动关闭,我不会自动关闭,我不会自动关闭,我不会自动关闭,我不会自动关闭',
duration: 0
};
notification.open(args);
};
ReactDOM.render(
<Button type="primary" onClick={openNotification}>打开通知提醒框</Button>
, mountNode);
import { Button, notification } from 'antd';
const close = function() {
console.log('我被默认的关闭按钮关闭了!');
};
const openNotification = function() {
const args = {
message: '这是标题',
description: '这是提示框的文案这是提示框示框的文案这是提示是提示框的文案这是提示框的文案',
onClose: close
};
notification.open(args);
};
ReactDOM.render(
<div>
<Button type="primary" onClick={openNotification}>打开通知提醒框</Button>
</div>,
mountNode);
notification.success(config)
notification.error(config)
notification.info(config)
notification.warn(config)
notification.open(config)
notification.close(key: String)
config 参数如下:
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
message | 通知提醒标题,必选 | React.Element or String | 无 |
description | 通知提醒内容,必选 | React.Element or String | 无 |
btn | 自定义关闭按钮 | React.Element | 无 |
key | 当前通知唯一标志 | String | 无 |
onClose | 点击默认关闭按钮时触发的回调函数 | Function | 无 |
duration | 默认 4.5 秒后自动关闭,配置为 null 则不自动关闭 | Number | 4.5 |
还提供了一个全局配置方法,需要在调用前提前配置,一次有效。
notification.config(options)
message.config({
top: 100
});
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
top | 消息距离顶部的位置 | Number | 24px |