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.
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
Installation
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.
Related
- Basic Promise Toast SonnerA promise toast that shows Loading
- Promise Toast with Data SonnerA promise toast whose success message
- Loading Toast SonnerA loading toast with a spinner that stays
- Toast with Action Button SonnerA toast with an Undo action button that
- Required Field Validation FormA name and an email field marked with
- Toast with Custom Duration SonnerA toast that stays on screen for 10