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 aToastContextValue 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 displayoptions(ToastOptions, optional):title(string, optional): Optional title above the messageduration(number, optional): Custom duration in milliseconds
(message: string, options?: ToastOptions) => void
Display an error toast notification.Parameters:
message(string): The error message to displayoptions(ToastOptions, optional):title(string, optional): Optional title above the messageduration(number, optional): Custom duration in milliseconds
(message: string, options?: ToastOptions) => void
Display an informational toast notification.Parameters:
message(string): The informational message to displayoptions(ToastOptions, optional):title(string, optional): Optional title above the messageduration(number, optional): Custom duration in milliseconds
(message: string, options?: ToastOptions) => void
Display a warning toast notification.Parameters:
message(string): The warning message to displayoptions(ToastOptions, optional):title(string, optional): Optional title above the messageduration(number, optional): Custom duration in milliseconds
(id: string) => void
Remove a specific toast by its ID.Parameters:
id(string): The unique identifier of the toast to remove
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 ofToastProvider:
ToastProvider: