Guide
Toast notifications in React
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
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.
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.
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.
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
durationin milliseconds to change that, or0to 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:
For the common case there is toast.promise, which does the same three steps for you and hands the promise back:
The returned promise still rejects when yours does, so keep your own try/catch around it. More patterns are in the loading toast guide.
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.
A toast with a button stays open until someone uses or dismisses it. The undo toast guide 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:
See styling, held modes and sound in the docs, or try looks in the 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 docsSetup, options, styling, sound and accessibility.
- Next.js toast notifications in the App Router (Boing)Toasts in a server-first app.
- Loading toastOne toast from loading to done.
- Undo toastAct first, offer undo.