useNotifications API
useNotifications Hook 的 API 參考。
Import
import useNotifications from '@toolpad/core/useNotifications';
// or
import { useNotifications } from '@toolpad/core';
請閱讀這篇指南,瞭解差異以盡可能縮小套件大小。
Usage
透過調用 Hook 來存取 notifications API。
const notifications = useNotifications();
Reference
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