shadcn/ui Sonner toast not showing: five causes and fixes
toast() firing with nothing on screen? Check the five causes: missing <Toaster />, wrong import, next-themes in Vite/Astro, Server Components, and CSS tokens.
- shadcn 4.23.0
- Tailwind CSS 4.3.5
- Vite 8
- React 19.3
- sonner 2.0
When you call toast("Saved") in a shadcn/ui project and nothing appears — with no console error — one of five things is wrong: <Toaster /> is missing from your root layout, sonner.tsx imports useTheme from next-themes without a provider, the wrong toast function was imported, or a modal's z-index or CSS import is hiding it.
Check these five things in order
Step 1: Render <Toaster /> once in your root layout
Running
npx shadcn@latest add sonneraddscomponents/ui/sonner.tsx, but does not mount it. Add<Toaster />inside your root layout so it stays mounted across routes:app/layout.tsxTSX import { Toaster } from "@/components/ui/sonner"; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en"> <body> {children} <Toaster /> </body> </html> );}Step 2: Import toast from "sonner", not from components/ui/sonner
components/ui/sonner.tsxonly exports the themedToastercomponent. The trigger function always comes from thesonnerpackage itself:save-button.tsxTSX "use client";import { toast } from "sonner"; export function SaveButton() { return <button onClick={() => toast.success("Changes saved")}>Save</button>;}Step 3: Vite or Astro without next-themes
The default shadcn
sonner.tsxcallsuseTheme()fromnext-themes. In a Vite or Astro project withoutThemeProvider,themecan beundefinedor throw at runtime. Pass a static theme (or your own hook) instead:src/components/ui/sonner.tsxTSX <Sonner theme="system" className="toaster group" {...props} />In Astro, remember to hydrate the island with
<Toaster client:load />— without aclient:*directive, Astro renders static HTML and the toast listener never attaches.Step 4: Calling toast inside a Server Component or Server Action
toast()only runs in the browser. Trigger it from a Client Component (with"use client"at the top) after your Server Action resolves, never inside the server function itself.
Questions
Should I use Sonner or the old shadcn Toast component?
Use Sonner. shadcn/ui deprecated its Radix-based toast component in favour of sonner, which needs no hook boilerplate, stacks cleanly and supports promise toasts out of the box.
Why do toasts appear twice in development?
You either mounted <Toaster /> in both a root layout and a nested layout, or called toast() directly inside a useEffect under React Strict Mode. Keep a single <Toaster /> at the root and fire toasts from event handlers.
Related articles
- shadcn/uishadcn/ui not working with Tailwind v4: six causes and fixesUnstyled or transparent shadcn components on Tailwind v4 come from v3 leftovers: the old PostCSS plugin, @tailwind directives, hsl() wrappers, missing imports.
- shadcn/uishadcn/ui dark mode not working? Check these seven thingsIs the dark class on <html>? If not: no provider, a flash on load, or Astro view transitions. If it is: colours without a dark value or @theme without inline.
- shadcn/uishadcn/ui animations not working on Tailwind v4Dialogs and accordions without animation mean a missing tw-animate-css or shadcn/tailwind.css import. Why the second one fails silently, and how to check.
Still stuck? We answer every message ourselves. Write to us