# Next.js toast notifications in the App Router (Boing)

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.

## Add the Toaster to the root layout

```bash
npm install react-boing
```

```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'`:

```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.

```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 };
}
```

```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.

> **Note:** An action that ends in `redirect()` never returns to your handler. To confirm it on the next page, pass a flag along (a search parameter, for example) and show the toast from a client component there.

## 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:

```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](https://boing.aayushbharti.in/docs/#csp).

## 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.

## Related guides

- [React toast notifications](https://boing.aayushbharti.in/react-toast/): One component and one function call.
- [shadcn/ui toast](https://boing.aayushbharti.in/shadcn-toast/): Fits the shadcn look out of the box.
- [Loading toast](https://boing.aayushbharti.in/loading-toast/): One toast from loading to done.
- [Boing docs](https://boing.aayushbharti.in/docs/): Setup, options, styling, sound and accessibility.

Source: https://boing.aayushbharti.in/nextjs-toast/
