Skip to content
boing

Guide

Toast notifications in the Next.js App Router

In the Next.js App Router, render <Toaster /> from react-boing in app/layout.tsx and call toast from client components. The package is already marked "use client", so the server layout can render it without a wrapper.

Try it

Add the Toaster to the root layout

Terminal
npm install react-boing
app/layout.tsx
import { Toaster } from 'react-boing';

/** Root layout: the page plus one Toaster shared by every route. */
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Toaster position="bottom-right" />
      </body>
    </html>
  );
}

The Toaster does not touch the DOM while rendering, so it works in static, dynamic and streamed routes. Because it lives in the root layout, a toast shown on one page stays on screen through client-side navigation to the next.

Call toast from a client component

Toasts happen in the browser, so toast is called from client code, usually an event handler in a file that starts with 'use client':

app/copy-link.tsx
'use client';

import { toast } from 'react-boing';

/** Copies the page URL and confirms it with a toast. */
export function CopyLink() {
  /** Writes the URL to the clipboard and reports the result. */
  async function copy() {
    try {
      await navigator.clipboard.writeText(location.href);
      toast.success('Link copied');
    } catch {
      toast.error('Could not copy the link');
    }
  }

  return (
    <button type="button" onClick={copy}>
      Copy link
    </button>
  );
}

Calling toast inside a server component or a server action does nothing useful: that code runs on the server, where there is no Toaster to show it.

How do I show a toast after a server action?

Have the action return a result object, then turn that result into a toast in the client component that called it.

app/projects/actions.ts
'use server';

type RenameResult = { ok: true; name: string } | { ok: false; error: string };

/** Renames a project. Returns the new name, or a message that is safe to show the user. */
export async function renameProject(formData: FormData): Promise<RenameResult> {
  const name = String(formData.get('name') ?? '').trim();
  if (!name) {
    return { ok: false, error: 'Give the project a name.' };
  }
  await saveProjectName(name); // your data layer
  return { ok: true, name };
}
app/projects/rename-form.tsx
'use client';

import { toast } from 'react-boing';
import { renameProject } from './actions';

/** Form that calls the rename action and shows its result as a toast. */
export function RenameForm() {
  /** Submits the form to the server action and maps the result to a toast. */
  async function submit(formData: FormData) {
    const result = await renameProject(formData);
    if (result.ok) {
      toast.success('Project renamed', { description: result.name });
    } else {
      toast.error(result.error);
    }
  }

  return (
    <form action={submit}>
      <input name="name" aria-label="Project name" />
      <button type="submit">Rename</button>
    </form>
  );
}

If you want a loading state while the action runs, start with toast.loading and pass the id to toast.update once the result arrives. For an action that throws instead of returning an error, toast.promise(renameProject(formData), { … }) covers all three steps.

Pass a CSP nonce

The Toaster adds one <style> element to <head>. If your Content Security Policy only allows inline styles with a nonce, read the nonce in the layout and pass it on. This example assumes your middleware or proxy puts it in an x-nonce request header:

app/layout.tsx
import { headers } from 'next/headers';
import { Toaster } from 'react-boing';

/** Root layout that forwards the request's CSP nonce to the Toaster's style tag. */
export default async function RootLayout({ children }: { children: React.ReactNode }) {
  const nonce = (await headers()).get('x-nonce') ?? undefined;

  return (
    <html lang="en">
      <body>
        {children}
        <Toaster nonce={nonce} />
      </body>
    </html>
  );
}

Reading headers makes the layout render per request, which a nonce needs anyway. If the nonce is wrong and the styles are blocked, icons keep their own small width and height. More in the docs.

Frequently asked questions

Do I need a client wrapper component for the Toaster in Next.js?

No. The published files start with a "use client" directive, so app/layout.tsx can import and render <Toaster /> directly.

Can a server action show a toast by itself?

No. A server action runs on the server. Return a result from it and call toast.success or toast.error in the client component that awaited it.

Does the toast survive client-side navigation?

Yes, when the Toaster is in the root layout. Layouts stay mounted across navigations, so the visible toast keeps its place and its timer.

Does it work with the Pages Router?

Yes. Render <Toaster /> in pages/_app.tsx and call toast from your components the same way.