ui: Toast is not showing

When i use the default code for showing a toast it does not show on the page. Did I miss something or do I need to add some extra logic for toasts to show.

I added the following code to my client component:

<Button
	variant='outline'
	onClick={() => {
		toast({
			title: 'Uh oh! Something went wrong.',
			description: 'There was a problem with your request.',
		});
	}}>
	Show Toast
</Button>

Im working on a nextjs project with ts. not sure if you need more information.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19

Most upvoted comments

@Default-01 Did you put the Toaster component in the layout.tsx?

Your layout.tsx should have a Toaster like this as mentioned in the docs. Hope that solves it

  import { Toaster } from "@/components/ui/toaster"
  export default function RootLayout({ children }) {
    return (
      <html lang="en">
        <head />
        <body>
          <main>{children}</main>
          <Toaster />
        </body>
      </html>
    )
  }

@Default-01 You can add 'use client' in the toaster.jsx|tsx file at ‘@/components/ui/toaster’. That way you don’t need to add 'use client' in your layout file. Should fix this issue.

Yes, the useToast() and toast({}) must be in a client component, but the Toaster should be in the layout.tsx, like the example below:

import "@/styles/globals.css";

import { Toaster } from "@/components/ui/Toaster";

interface RootLayoutProps {
  children: React.ReactNode;
}

export default async function RootLayout({
  children,
}: RootLayoutProps) {
  return (
    <html>
      <body className="min-h-screen bg-background antialiased">
          <main className="relative flex min-h-screen flex-col">
            <div className="flex flex-1 flex-col">{children}</div>
          </main>
          <Toaster />
      </body>
    </html>
  );
}

Yes, the useToast() and toast({}) must be in a client component, but the Toaster should be in the layout.tsx, like the example below:

import "@/styles/globals.css";

import { Toaster } from "@/components/ui/Toaster";

interface RootLayoutProps {
  children: React.ReactNode;
}

export default async function RootLayout({
  children,
}: RootLayoutProps) {
  return (
    <html>
      <body className="min-h-screen bg-background antialiased">
          <main className="relative flex min-h-screen flex-col">
            <div className="flex flex-1 flex-col">{children}</div>
          </main>
          <Toaster />
      </body>
    </html>
  );
}

You are an Angel!