shadcn.io is not affiliated with official shadcn/ui
Sonner examples · Promise
Promise Toast with Data Sonner for React
A promise toast whose success message names the user the promise resolved with.
The success message can use what the promise resolved with. Here success is a function of the result, so after 2 seconds Creating user... becomes John Doe has been added.
Preview
Installation
FAQ
How does the success message get the name?
success is a function, (data) => the string with data.name and "has been added". The promise resolves after 2 seconds with { name: "John Doe" }, so the toast becomes "John Doe has been added".
What shows while the promise is pending?
loading is "Creating user...". That line is on screen until the promise resolves, in the same toast. toast.promise then swaps in the string the success function returns.
What is the error message in this file?
The string "Failed to create user". This button's promise always resolves, so the toast uses the success line. A rejection would show that error string as written.
What should the success function return?
A string. This file reads data.name from the resolved object and returns that sentence. The call also omits description.
Related
- Basic Promise Toast SonnerA promise toast that shows Loading
- Promise Toast with Error SonnerA promise toast that fails after 2
- Loading Toast SonnerA loading toast with a spinner that stays
- Toast with Action Button SonnerA toast with an Undo action button that
- Toast with Custom Duration SonnerA toast that stays on screen for 10
- Toast with Custom Icon SonnerA toast with its own check icon in place