useToast@astryxdesign/core v0.7.17 · useToast

Usage

Hook for showing toast notifications from anywhere in your component tree; it needs no provider or mounted ToastViewport: inside LayerProvider (or a ToastViewport) it uses that viewport, and otherwise the first toast mounts a fallback viewport on document.body with a development warning. Returns the show function itself (not an object): const showToast = useToast(); call it with toast options to show the notification.

ts
import {useToast} from '@astryxdesign/core/Toast'

Best practices

GuidancePractices
Do

Use for transient success/error feedback that does not require user action.

Do

Set uniqueID to deduplicate toasts from rapid user actions.

Do

For an undo action, pass endContent: <Button label="Undo" variant="ghost" onClick={...} />, and call the dismiss function that showToast returns from that onClick.

Don't

Use for critical errors that require acknowledgment; use AlertDialog instead.

Don't

Call useToast in the same component that renders LayerProvider; it must be called from a child component inside the provider.

Returns

FieldTypeDescription
value(options:
) => () => void

useToast returns the show function itself, not an object: const showToast = useToast(); showToast({body: 'Saved'});. Call it with toast options to show a toast; it returns a function that dismisses that toast. ToastOptions fields: body: ReactNode (required) is the message. type?: "info" | "error" sets the color; default "info". isAutoHide?: boolean dismisses the toast on a timer; default true for "info" and false for "error". autoHideDuration?: number is the time in milliseconds before the auto-dismiss; default 5000. endContent?: ReactNode is trailing content, for example an Undo Button. uniqueID?: string identifies the toast for deduplication. collisionBehavior?: "overwrite" | "ignore" sets what a new toast with the same uniqueID does: "overwrite" replaces the shown toast in place, "ignore" keeps the shown toast and drops the new one; default "overwrite". onHide?: (reason: "auto" | "manual") => void runs when the toast is removed. renderContent?: (toast) => ReactNode replaces the content of the card with your own layout; it receives body, endContent, type, isAutoHide, autoHideDuration and dismiss. Your layout must include the control that closes the toast. Import the ToastOptions type from @astryxdesign/core/Toast.

Examples

Common configurations, variations, and states.
Toast
Open in Playground

Imperative toast notifications triggered with useToast and rendered in the toast viewport.

useToast — Settings Saved
Open in Playground

Shows a toast from the function useToast returns: a "Settings saved" confirmation with an Undo action, a uniqueID so pressing the button again replaces the toast instead of stacking another, and an error toast when the request fails.

Use with shadcn

Already using the shadcn registry workflow? Install the real Astryx package and a local public re-export. Component implementation source stays in Astryx.

This install URL expires with the draft preview.
bash
npx shadcn@4.19.0 add https://astryx-pfdtepiwo-fbopensource.vercel.app/shadcn/hooks/use-toast.json