Introducing PrimeReact v11-alpha 🎉Discover Now
styledMessages

Toast

Toast is a component that displays a message to the user.

preview

Usage#

import { Toast } from '@primereact/ui/toast';
import { Toaster, toast } from '@primereact/ui/toaster';
<Toaster.Root>
    <Toaster.Portal>
        <Toaster.Region>
            {({ toaster }) =>
                toaster?.toasts?.map((toast) => (
                    <Toast.Root key={toast.id} toast={toast}>
                        <Toast.Icon />
                        <Toast.Title />
                        <Toast.Description />
                        <Toast.Action />
                        <Toast.Close />
                    </Toast.Root>
                ))
            }
        </Toaster.Region>
    </Toaster.Portal>
</Toaster.Root>

Examples#

Variants#

Use toast function to create a toast with different variants.

variants-demo

Position#

Use position prop to change the position of the toast.

position-demo

Action#

Use the action prop to add an interactive button to the toast.

action-demo

toast function#

remove method#

The remove method removes a toast by its ID by animating it out. If id is not provided, it will remove all toasts.

toast.remove(id);

delete method#

The delete method removes a toast by its ID without animating it out. If id is not provided, it will remove all toasts.

toast.delete(id);

update method#

The update method updates a toast by its ID. It accepts an object with the properties to update.

const id = toast.loading({
    title: 'Please wait.',
    description: 'This will take a moment'
    // ...
});

Change the toast options by its ID.

toast.update(id, {
    title: 'Success',
    description: 'The operation was successful',
    variant: 'success'
    // ...
});

promise method#

The promise method is a wrapper around the toast function that creates a toast based on the result of a promise. Each state has its own toast configuration.

toast.promise(Promise, {
    loading: {
        title: '',
        description: ''
        // ...
    },
    success: {
        title: '',
        description: ''
        // ...
    },
    error: {
        title: '',
        description: ''
        // ...
    }
});

custom method#

The custom method creates a toast that is based on a custom component. Also, it accepts other props from toast function.

toast.custom(<div>Hello</div>, {
    duration: 1000
    // ...
});

action prop#

The action prop accepts all Toast.Action component properties - any props passed will be directly forwarded to the underlying Toast.Action component, including children, onClick, className, and other Button-specific properties.

toast({
    title: '',
    description: '',
    action: {
        children: '',
        onClick: () => {},
        className: ''
        // ...
    }
});

duration prop#

The duration prop is the duration of the toast in milliseconds. Passing Infinity keeps the toast visible until it is removed manually.

toast({
    duration: 1000
    // ...
});

icon prop#

The icon prop accepts a React node or a function that returns a React node.

toast({
    icon: <CheckIcon />
    // ...
});

close callbacks#

onDismiss and onTimeout callbacks are called when the toast is dismissed or timed out.

toast({
    onDismiss: () => {},
    onTimeout: () => {}
    // ...
});

group prop#

Add group to the toast to group it with other toasts.

toast({
    group: 'group-1'
    // ...
});

API#

Sub-Components#

See Toast Primitive API for the full sub-component API reference.

Hooks#

See Toast Headless API for the hook API reference.

Accessibility#

See Toast Primitive for WAI-ARIA compliance details and keyboard support.