Docs
Toast
Toast
A succinct message that is displayed temporarily.
Installation
npx shadcn-ui add toast
npx shadcn-ui add toast
Toaster
Add the <Toaster />
component to your app.
The <Toaster />
component is where your toasts are displayed. You can place it anywhere in your app, but it's recommended to place it at the root of your app.
Pages
_app.tsx
import { Toaster } from "@/components/ui/toaster"
export default function App({ Component, pageProps }) {
return (
<>
<Component {...pageProps} />
<Toaster />
</>
)
}
_app.tsx
import { Toaster } from "@/components/ui/toaster"
export default function App({ Component, pageProps }) {
return (
<>
<Component {...pageProps} />
<Toaster />
</>
)
}
App Directory
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>
)
}
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
The useToast
hook returns a toast
function that you can use to display a toast.
import { useToast } from "@/components/ui/use-toast"
import { useToast } from "@/components/ui/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>
)
}
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>
)
}
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
Use toast({ variant: "destructive" })
to display a destructive toast.