"use client";
import { useToast } from "@/hooks/use-toast";
import { Button } from "@/components/ui/button";
import { ToastAction } from "@/components/ui/toast";
export default function ToastDemo() {
const { toast } = useToast();
return (
<Button
variant="outline"
onClick={() => {
toast({
title: "Scheduled: Catch up",
description: "Friday, February 10, 2023 at 5:57 PM",
action: <ToastAction altText="Goto schedule to undo">Undo</ToastAction>,
});
}}
>
Add to calendar
</Button>
);
}
Installation
npx nore-ui-cli@latest add toastUsage
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>
);
};
To display multiple toasts at the same time, you can update the TOAST_LIMIT in use-toast.ts.
Examples
Simple
"use client";
import { useToast } from "@/hooks/use-toast";
import { Button } from "@/components/ui/button";
export default function ToastSimple() {
const { toast } = useToast();
return (
<Button
variant="outline"
onClick={() => {
toast({
description: "Your message has been sent.",
});
}}
>
Show Toast
</Button>
);
}
With Title
"use client";
import { useToast } from "@/hooks/use-toast";
import { Button } from "@/components/ui/button";
export default function ToastWithTitle() {
const { toast } = useToast();
return (
<Button
variant="outline"
onClick={() => {
toast({
title: "Uh oh! Something went wrong.",
description: "There was a problem with your request.",
});
}}
>
Show Toast
</Button>
);
}
With Actions
"use client";
import { useToast } from "@/hooks/use-toast";
import { Button } from "@/components/ui/button";
import { ToastAction } from "@/components/ui/toast";
export default function ToastWithAction() {
const { toast } = useToast();
return (
<Button
variant="outline"
onClick={() => {
toast({
title: "Uh oh! Something went wrong.",
description: "There was a problem with your request.",
action: <ToastAction altText="Try again">Try again</ToastAction>,
});
}}
>
Show Toast
</Button>
);
}
Danger
Use toast({ variant: "danger" }) to display a danger toast.
"use client";
import { useToast } from "@/hooks/use-toast";
import { Button } from "@/components/ui/button";
import { ToastAction } from "@/components/ui/toast";
export default function ToastDanger() {
const { toast } = useToast();
return (
<Button
variant="outline"
onClick={() => {
toast({
variant: "danger",
title: "Uh oh! Something went wrong.",
description: "There was a problem with your request.",
action: <ToastAction altText="Try again">Try again</ToastAction>,
});
}}
>
Show Toast
</Button>
);
}