跳到内容
+

useNotifications API

useNotifications 钩子的 API 参考。

导入

import useNotifications from '@toolpad/core/useNotifications';
// or
import { useNotifications } from '@toolpad/core';

阅读这篇关于减小 bundle 大小的指南,了解它们之间的区别。

用法

通过调用钩子来访问 notifications API。

const notifications = useNotifications();

参考

notifications.show

调用 notifications.show 方法以在应用程序中显示 snackbar。

const notification = notifications.show('Something great just happened!', {
  severity: 'success',
});

签名

function show(message, options?)

参数

  • message: React.ReactNode 要在 snackbar 中显示的消息。
  • options?: object 用于配置通知的选项对象。 -key?: string 用于去重通知的键。如果未提供,将生成一个唯一的键。
    • severity?: 'info' | 'warning' | 'error' | 'success' 通知的严重程度。如果提供,snackbar 将显示具有指定严重程度的警报。默认为 undefined
    • autoHideDuration?: number 通知在多少毫秒后自动关闭。默认情况下,通知不会自动隐藏。
    • actionText?: React.ReactNode 要在操作按钮上显示的文本。
    • onAction?: () => void 单击操作按钮时要调用的回调。

返回值

可用于以编程方式关闭通知的唯一键。

notifications.close

以编程方式关闭通知。

const notification = notifications.show(/* ... */);

// ...

notifications.close(notification);

签名

function close(notification)

参数

  • key: string

返回值

void