Toast 是一种轻量的反馈组件,用于显示操作结果、提示信息等。
在应用根组件中添加 <Toaster />,然后使用 toast() 函数显示提示。
支持多种消息类型:default、success、info、warning、error。
可以显示标题和描述信息。
可以控制 toast 显示的位置。
| 属性 | 类型 | 说明 |
|---|---|---|
| message | ReactNode |
提示消息内容 |
| options | ToastOptions |
配置选项 |
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| type | "default" | "success" | "info" | "warning" | "error" |
"default" |
消息类型 |
| title | ReactNode |
- | 标题 |
| description | ReactNode |
- | 描述文字 |
| action | { label: string, onClick: () => void } |
- | 操作按钮 |
| duration | number |
4000 |
显示时长(毫秒),设为 0 可禁用自动关闭 |
| position | ToastPosition |
"bottom-right" |
显示位置 |
"top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right"
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| position | ToastPosition |
"bottom-right" |
默认 toast 位置 |
app/layout.tsx)中添加 <Toaster /> 组件toast() 函数可以在任何组件中调用,无需传递上下文toast.dismiss() 关闭