Docs
Toast
Toast
暂时显示的简洁消息。
安装
¥Installation
Run the following command:
pnpm dlx shadcn@latest add toast
Add the Toaster component
app/layout.tsx
import { Toaster } from "@/components/ui/toaster"
export default function RootLayout({ children }) {
return (
<html lang="en">
<head />
<body>
<main>{children}</main>
<Toaster />
</body>
</html>
)
}
用法
¥Usage
useToast
钩子返回一个 toast
函数,你可以使用它来显示提示。
¥The useToast
hook returns a toast
function that you can use to display a toast.
import { useToast } from "@/hooks/use-toast"
export const ToastDemo = () => {
const { toast } = useToast()
return (
<Button
onClick={() => {
toast({
title: "Scheduled: Catch up",
description: "Friday, February 10, 2023 at 5:57 PM",
})
}}
>
Show Toast
</Button>
)
}
要同时显示多个提示,你可以在 use-toast.tsx
中更新 TOAST_LIMIT
。
¥To display multiple toasts at the same time, you can update the TOAST_LIMIT
in use-toast.tsx
.
示例
¥Examples
简单
¥Simple
带有标题
¥With title
带有操作
¥With Action
破坏性
¥Destructive
使用 toast({ variant: "destructive" })
显示破坏性提示。
¥Use toast({ variant: "destructive" })
to display a destructive toast.