Skip to main content
The useToast hook provides access to the toast context, allowing you to display notifications from anywhere in your React application.

Usage

The useToast hook must be used within a component that is a child of ToastProvider. It will throw an error if used outside the provider context.

Return value

The hook returns a ToastContextValue object with the following properties and methods:

Properties

ToastItem[]
Array of currently active toast items. Each item contains:
ToastConfig
The current toast configuration:

Methods

(message: string, options?: ToastOptions) => void
Display a success toast notification.Parameters:
  • message (string): The message to display
  • options (ToastOptions, optional):
    • title (string, optional): Optional title above the message
    • duration (number, optional): Custom duration in milliseconds
Example:
(message: string, options?: ToastOptions) => void
Display an error toast notification.Parameters:
  • message (string): The error message to display
  • options (ToastOptions, optional):
    • title (string, optional): Optional title above the message
    • duration (number, optional): Custom duration in milliseconds
Example:
(message: string, options?: ToastOptions) => void
Display an informational toast notification.Parameters:
  • message (string): The informational message to display
  • options (ToastOptions, optional):
    • title (string, optional): Optional title above the message
    • duration (number, optional): Custom duration in milliseconds
Example:
(message: string, options?: ToastOptions) => void
Display a warning toast notification.Parameters:
  • message (string): The warning message to display
  • options (ToastOptions, optional):
    • title (string, optional): Optional title above the message
    • duration (number, optional): Custom duration in milliseconds
Example:
(id: string) => void
Remove a specific toast by its ID.Parameters:
  • id (string): The unique identifier of the toast to remove
Example:
You rarely need to use this method directly, as toasts auto-dismiss and have close buttons.
() => void
Remove all active toasts immediately.Example:

TypeScript types

Examples

Basic toast notifications

Toast with title

Custom duration

Form validation

Clearing toasts on navigation

Accessing current toasts

Error handling

The hook throws an error if used outside of ToastProvider:
Always ensure your components are wrapped with ToastProvider: