react-hot-toast
react-hot-toast copied to clipboard
feat: add optional "finally" callback after promise completion
The objective of this simple code increment is to allow execute some code ONLY after promise.toast
completes its execution.
In my scenario it's useful to control UI elements that I want to keep disabled while the promise is running, disabling a button to be more specific, and reenabling the button with the "finally" callback, no matter if the promise resolves or rejects.
Example code:
setIsCreatingTodo(true); // one UI button is disabled while todo is being created
toast.promise(createTodo()), {
loading: 'Loading...',
success: (createdTodo) => {
gotoTodoViewer(createdTodo);
return 'success';
},
error: err => {
const error = err as Error;
return error.message;
},
finally: () => {
setIsCreatingTodo(false); // the button can be reactivated
}
});