Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Sonner examples · Promise

Promise Toast with Error Sonner for React

A promise toast that fails after 2 seconds and shows the error's message.

Framework
UI library

A failed task should say what went wrong. Here error is a function of the rejection, so after 2 seconds Saving changes... becomes Error: Network error.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

How does the error line get the message?

error is a function, (error: Error) => a string that reads error.message. The promise rejects after 2 seconds with new Error("Network error"), so the toast becomes Error: Network error.

What shows before the rejection?

loading is "Saving changes...". That stays on the same toast until the promise rejects. The error function's string then replaces it.

Is the success string used?

success is "Changes saved successfully", and this button's promise always calls reject. A resolve would show that string as written.

What does the error function receive?

The rejection from the promise. This demo rejects with an Error, then reads message. The function returns a string, which is the toast title.

How long does the error toast stay?

This call does not pass duration. After the swap off the loading type, the toast uses duration on Toaster, or 4 seconds when that is omitted.

Where does the spinner come from?

Toaster's icons.loading, a Loader2Icon with animate-spin. The demo renders its own Toaster beside the button; in an app, render one near the root.