useToast
Shows, updates and closes toasts from anywhere below a Toaster.
import { useToast } from "@wunderui/react"Playground
Installation
useToast is part of WunderUI Free, in the npm package and in the free source. Import it from :
import { useToast } from "@wunderui/react"When to use
Use useToast for
- Confirming something that already happened: a saved profile, a copied link, a sent invitation.
- Reporting the result of a request that runs in the background, with .
- A short message with one optional action, such as Undo.
Signature
Returns the toast manager of the nearest . Call with a title, an optional description and a type, and the toast slides into the corner the Toaster was given. returns the toast's id, which and take.
useToast<Data>(): UseToastManagerReturnValue<Data>Returns
The toast manager of the nearest . The functions keep their identity between renders, so they are safe in effect dependencies; the object itself changes whenever the list of toasts does.
Behavior and motion
The hook is the remote control; the toasts live in the Toaster.
- State
- reads the manager of the nearest . Every component that calls it re-renders when the list of toasts changes, so keep the call in a small component, such as the button that triggers the toast, when the rest of the tree is heavy.
- Timing
- A toast closes after 5 seconds, the default of the Toaster. on changes it for one toast, keeps it until it is closed. The timers pause while the pointer or the focus is inside the stack and while the browser window is in the background.
- Stack and corner
- Toasts stack in the corner the Toaster was given, by default or . The newest sits in front, older ones lie a little lower, smaller and fainter, and the stack opens into a list while the pointer is on it. From the fifth toast on, the older ones are hidden until a newer one closes.
- Server rendering
- , , and run in the browser, in event handlers or effects. The viewport renders into a portal after mount, so a server render of the page contains no toasts and never differs from the first client render.
- Cleanup
- A toast belongs to the Toaster, not to the component that added it. It stays when that component unmounts, and a pending still turns its toast into the result. Close what should not outlive a screen with in an effect cleanup.
- Announcements and keyboard
- Every toast is announced to screen readers: politely (the default), urgently, which suits errors the user must notice. Press F6 to move the focus into the stack; Tab then reaches the action and the close button of each toast, and Escape closes the toast that has the focus.
- Motion
- A toast slides in from the right edge and fades in over with , and leaves the same way; the stack offsets and scales with the same timing when it opens. Only and change.
- Reduced motion
- Under the transform transitions are dropped, so a toast no longer slides or shifts: it fades in and out in place.
Best practices
- Do
Write what happened in the title (“Changes saved”) and put the consequence or the next step in the description.
- Don’t
Do not put a decision in a toast. It disappears after 5 seconds, and a person who looked away cannot answer it.
- Do
Use with for failures the user has to notice, and keep the message actionable: say what to try next.
- Don’t
Do not stack a toast per item in a loop. Add one toast that says how many items were affected.
- Do
Give a destructive action an Undo through and a longer , instead of asking for confirmation first.
- Don’t
Do not call during render. Call it in an event handler, an effect or after an awaited request.
Notes for AI
Give your coding assistant the page or the MCP call instead of screenshots.
get_component("useToast")import { useToast } from "@wunderui/react"The full library index for assistants is at /llms.txt.