Skip to content
Drivn
5 min read

React Toast Component Examples

Copy-paste React Toast examples: success and error toasts, a toast with a description, an action button with Undo, and positioning the Toaster.

Toasts confirm that something happened, such as a saved draft, a sent invite, or a deleted file, and then get out of the way. Drivn's Toast is a short wrapper around Sonner. The file that npx drivn add toast writes to @/components/ui/toast exports two things: a Toaster that renders Sonner's container with Drivn defaults, and the toast function, re-exported unchanged. You mount Toaster once and call toast() from any client event handler.

The defaults are why the wrapper exists. Toaster passes Sonner an icons map with five lucide-react icons, CheckCircle2, Info, AlertTriangle, XCircle, and a spinning Loader2, each sized w-4 h-4. It also sets Sonner's CSS variables from Drivn tokens: --card for the background, --card-foreground for the text, --border for the outline, and a 12px radius. A toast therefore looks like a Card in both themes without extra classes.

Sonner is the one runtime package this component adds, and the wrapper around it is source you own and can edit. The examples below are TypeScript, matching the .tsx file the Drivn CLI installs. They cover success and error toasts, the typed variants with descriptions, an Undo action, and moving the toast region by editing the Toaster itself.

A basic success and error toast

The simplest use is two buttons that fire a typed toast. Mount the Toaster once — it renders the Sonner toast region — then call toast.success or toast.error from any click handler. Each typed call shows its pre-wired lucide icon: a CheckCircle2 for success and an XCircle for error, both sized w-4 h-4 against the message.

The wrapper is a client component because the buttons attach onClick handlers, and the Toaster only needs to be mounted once in your tree — placing it in your root layout means every page can call toast() without mounting its own. The toast auto-dismisses on Sonner's default timer, and stacks with any other active toasts. This is the canonical pattern for confirming a completed action.

1"use client"
2
3import { Toaster, toast } from "@/components/ui/toast"
4
5export default function Example() {
6 return (
7 <div>
8 <button onClick={() => toast.success("Saved successfully")}>
9 Success
10 </button>
11 <button onClick={() => toast.error("Something went wrong")}>
12 Error
13 </button>
14 <Toaster />
15 </div>
16 )
17}

The full set of typed variants

Beyond success and error, the toast function exposes info and warning, each with its own pre-wired icon — Info and AlertTriangle respectively — plus a plain toast() call for a neutral message with no icon. Every variant accepts the same options object, so you can attach a description second line to any of them, as the success and error examples below show.

This covers the common notification taxonomy: a neutral default for "Changes saved", success for a confirmed write, error for a failed operation, info for a non-urgent heads-up, and warning for something the user should notice but that is not a failure. Each maps to the icon Drivn wired in its Toaster, so the visual language stays consistent across your whole app without any per-call icon wiring.

1<Button variant="secondary" size="sm" onClick={() => toast("Changes saved")}>
2 Default
3</Button>
4<Button variant="secondary" size="sm" onClick={() => toast.success("Published!", { description: "Your post is now live." })}>
5 Success
6</Button>
7<Button variant="secondary" size="sm" onClick={() => toast.error("Something went wrong", { description: "Please try again." })}>
8 Error
9</Button>
10<Button variant="secondary" size="sm" onClick={() => toast.info("New update available")}>
11 Info
12</Button>
13<Button variant="secondary" size="sm" onClick={() => toast.warning("Low storage space")}>
14 Warning
15</Button>

A toast with an action button

For toasts the user can act on — the classic "deleted, but Undo" pattern — pass an action object as the second argument with a label and an onClick. Sonner renders the label as an inline button inside the toast; clicking it runs your handler and dismisses the toast. Drivn inherits this API unchanged because it re-exports toast directly from Sonner.

This is the right pattern for any reversible destructive action: delete the item optimistically, show the toast with an Undo action, and only commit the deletion if the toast dismisses without the action being clicked. It keeps the main flow fast — no confirm dialog — while still giving the user a safety net. Pair it with a Button elsewhere in the flow for the primary trigger.

1// Toast with action button
2toast.success("File deleted", {
3 action: {
4 label: "Undo",
5 onClick: () => restoreFile(),
6 },
7})

Moving the toast region to another corner

Sonner places its toast region with a position prop that accepts top-left, top-center, top-right, bottom-left, bottom-center, or bottom-right, and defaults to bottom-right. Drivn's wrapper is declared as function Toaster() with no props, so writing <Toaster position="top-center" /> will not type-check. Set the prop on the <Sonner> element inside the file instead. Because the component lives in your codebase, that is a one-line edit in @/components/ui/toast, not a fork of a dependency.

Set the position once and every toast() call in the app uses that corner. Top-center suits apps with a bottom navigation bar or a composer pinned to the bottom edge. Other Sonner options, such as duration, closeButton, or visibleToasts, go on the same element. To make them configurable per mount, type the function's props as React.ComponentProps<typeof Sonner> and spread them onto <Sonner> before icons and style, so Drivn's icons and theming still apply. Mount the edited Toaster in your root layout, next to the rest of your Drivn install.

1// @/components/ui/toast — registry source plus one added line
2function Toaster() {
3 return (
4 <Sonner
5 position="top-center"
6 icons={{
7 success: (
8 <CheckCircle2 className="w-4 h-4" />
9 ),
10 info: (
11 <Info className="w-4 h-4" />
12 ),
13 warning: (
14 <AlertTriangle className="w-4 h-4" />
15 ),
16 error: (
17 <XCircle className="w-4 h-4" />
18 ),
19 loading: (
20 <Loader2 className="w-4 h-4 animate-spin" />
21 ),
22 }}
23 style={{
24 '--normal-bg': 'var(--card)',
25 '--normal-text': 'var(--card-foreground)',
26 '--normal-border': 'var(--border)',
27 '--border-radius': '12px',
28 }}
29 />
30 )
31}
Get started

Install Drivn in one command

Copy the source into your project and own every line. Zero runtime dependencies, pure React + Tailwind.

npx drivn@latest create

Requires Node 18+. Works with npm, pnpm, and yarn.

Enjoying Drivn?
Star the repo on GitHub to follow new component releases.
Star →

Frequently asked questions

Install the component with npx drivn add toast, mount the Toaster once near your app root, then call toast() from any event handler. For typed toasts use toast.success, toast.error, toast.info, or toast.warning, each of which fires the matching message with its pre-wired lucide icon. The toast function is re-exported from Sonner, so the full Sonner API is available.

Mount the Toaster once, high in your tree — typically in your root layout — so every page can call toast() without rendering its own region. The Toaster renders the Sonner toast container that all toast() calls feed into. Mounting it more than once creates duplicate regions, so a single instance in the layout is the standard setup for a Next.js app.

Pass a description string in the options object as the second argument, for example toast.success("Published!", { description: "Your post is now live." }). The description renders as a secondary line beneath the main message. Every toast variant — default, success, error, info, and warning — accepts the same options object, so descriptions work identically across all of them.

Pass an action object as the second argument with a label and an onClick handler, such as toast.success("File deleted", { action: { label: "Undo", onClick: () => restoreFile() } }). Sonner renders the label as an inline button; clicking it runs your handler and dismisses the toast. This is the standard pattern for reversible destructive actions like delete-with-undo.

Add Sonner's position prop to the Sonner element inside your Toaster file. Drivn's Toaster takes no props, so writing position on <Toaster /> will not type-check. Sonner accepts top-left, top-center, top-right, bottom-left, bottom-center, and bottom-right, with bottom-right as the default. Because the file is yours, the change is one line, and every toast in the app then appears in that corner.