# React toast notifications: install and use Boing

To show a toast in React, install `react-boing`, render `<Toaster />` once near the root, and call `toast.success('Saved')` from any event handler. There is no provider to wrap and no stylesheet to import.

## Install the package

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

React 18 or newer is the only peer dependency. The package has no runtime dependencies, and the `<Toaster />` adds its own styles to the page when it mounts.

## Render the Toaster once

Put one `<Toaster />` near the top of your component tree. It draws whatever toast is on screen and owns the live regions that screen readers listen to.

```tsx
import { createRoot } from 'react-dom/client';
import { Toaster } from 'react-boing';
import { App } from './App';

createRoot(document.getElementById('root')!).render(
  <>
    <App />
    <Toaster position="bottom-center" />
  </>,
);
```

The `position` prop takes one of six corners and edges. Leave it out and toasts appear at the top centre. The other props are `theme`, `labels` and `nonce`, all listed in [the docs](https://boing.aayushbharti.in/docs/#configure).

## How do I show a toast?

Import `toast` and call the shortcut for the kind of message you have. Each one takes a title and an optional options object, and returns the toast id.

```tsx
import { toast } from 'react-boing';

/** Saves the draft and reports the outcome in a toast. */
export function SaveButton({ save }: { save: () => Promise<void> }) {
  /** Runs the save and shows a success or error toast. */
  async function handleClick() {
    try {
      await save();
      toast.success('Draft saved');
    } catch {
      toast.error('Could not save', { description: 'Check your connection and try again.' });
    }
  }

  return (
    <button type="button" onClick={handleClick}>
      Save
    </button>
  );
}
```

The shortcuts are `success`, `error`, `warning`, `info` and `loading`. `toast` is a plain module-level store rather than a hook, so you can also call it from effects, data hooks or code outside React.

- Success, warning and info toasts close after 5 seconds.
- Three kinds wait for the user: errors, loading toasts, and any toast that has a button. They close when dismissed or updated.
- Pass `duration` in milliseconds to change that, or `0` to keep a toast open.

## Update a toast instead of adding another

Boing keeps one toast on screen. A new call while one is visible changes the visible toast into the new message, and you can also change a specific toast by id:

```ts
const id = toast.loading('Publishing…');

try {
  await publish();
  toast.update(id, { type: 'success', title: 'Published' });
} catch {
  toast.update(id, { type: 'error', title: 'Publishing failed' });
}
```

For the common case there is `toast.promise`, which does the same three steps for you and hands the promise back:

```ts
await toast.promise(publish(), {
  loading: 'Publishing…',
  success: 'Published',
  error: 'Publishing failed',
});
```

The returned promise still rejects when yours does, so keep your own `try`/`catch` around it. More patterns are in the [loading toast guide](https://boing.aayushbharti.in/loading-toast/).

## Add an undo button

Give the toast an `action`. When its `onClick` returns a patch, the toast turns into that patch; when it returns nothing, the toast closes.

```ts
toast.success('Comment deleted', {
  action: {
    label: 'Undo',
    onClick: () => {
      restoreComment(commentId);
      return { type: 'info', title: 'Comment restored' };
    },
  },
});
```

A toast with a button stays open until someone uses or dismisses it. The [undo toast guide](https://boing.aayushbharti.in/undo-toast/) covers when to use this instead of a confirm dialog.

## Style and configure

Colours, radius and spacing are CSS variables on `.boing-viewport`, such as `--boing-bg` and `--boing-radius`. Behaviour that has no prop, like sound or how several open toasts share the screen, goes through `toast.configure`:

```ts
toast.configure({ held: 'list', sound: true, volume: 0.3 });
```

See [styling](https://boing.aayushbharti.in/docs/#styling), [held modes](https://boing.aayushbharti.in/docs/#held) and [sound](https://boing.aayushbharti.in/docs/#sound) in the docs, or try looks in the [playground](https://boing.aayushbharti.in/playground/).

## Frequently asked questions

### Do I need a context provider to use toasts in React?

No. `toast` is a shared store and `<Toaster />` reads from it, so you render the Toaster once and import `toast` wherever you need it.

### Is there a stylesheet to add?

No. The Toaster inserts its stylesheet at the start of `<head>` when it mounts. Your own CSS loads later, so it overrides the defaults without `!important`.

### Can I show more than one toast at a time?

By default a new toast replaces the visible one by morphing it. Set `toast.configure({ held: 'list' })` to keep errors and other sticky toasts on screen while quick ones appear beside them.

### How do I close a toast from code?

Call `toast.dismiss(id)` with the id a shortcut returned, or `toast.dismiss()` with no id to close every toast.

## Related guides

- [Boing docs](https://boing.aayushbharti.in/docs/): Setup, options, styling, sound and accessibility.
- [Next.js toast notifications in the App Router (Boing)](https://boing.aayushbharti.in/nextjs-toast/): Toasts in a server-first app.
- [Loading toast](https://boing.aayushbharti.in/loading-toast/): One toast from loading to done.
- [Undo toast](https://boing.aayushbharti.in/undo-toast/): Act first, offer undo.

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