useToastState

Provides state management for a toast queue. Toasts display brief, temporary notifications of actions, errors, or other events in an application.

installyarn add react-stately
version3.36.0
usageimport {useToastState} from 'react-stately'

API#


useToastState<T>( (props: )): <T>

Interface#


Properties

NameTypeDescription
visibleToasts<T>[]The visible toasts.

Methods

MethodDescription
add( (content: T, , options?: )): stringAdds a new toast to the queue.
close( (key: string )): voidCloses a toast.
pauseAll(): voidPauses the timers for all visible toasts.
resumeAll(): voidResumes the timers for all visible toasts.

ToastQueue#


useToastState uses a ToastQueue under the hood, which means the queue is owned by the component that calls it. If you want to have a global toast queue outside the React tree, you can use the ToastQueue class directly. The hook can be used to to subscribe to a ToastQueue within a React component, and returns the same interface as described above.

Properties

NameTypeDescription
visibleToasts<T>[]The currently visible toasts.

Methods

MethodDescription
constructor( (options?: )): void
subscribe( (fn: () => void )): voidSubscribes to updates to the visible toasts.
add( (content: T, , options: )): voidAdds a new toast to the queue.
close( (key: string )): voidCloses a toast.
pauseAll(): voidPauses the timers for all visible toasts.
resumeAll(): voidResumes the timers for all visible toasts.

Example#


See the docs for useToast in react-aria for an example of useToastState.