Skip to content

Instantly share code, notes, and snippets.

@nelsoneldoro
Last active September 26, 2024 04:30
Show Gist options
  • Save nelsoneldoro/306aef4a975830868a47707fa30470f9 to your computer and use it in GitHub Desktop.
Save nelsoneldoro/306aef4a975830868a47707fa30470f9 to your computer and use it in GitHub Desktop.
react-toastify: dismiss as promise
import * as toast from './toast';
function SingleToast() {
return (
<>
<button onClick={() => toast.error('Error!', { toastId: 'single-toast' })}>OpenError</button>
<button
onClick={async () => {
await toast.dismiss('single-toast');
toast.success('Hey!', { toastId: 'single-toast' });
}}
>
Open Success
</button>
</>
);
}
import { toast } from 'react-toastify';
export function success(content, options) {
return toast.success(content, options );
}
export function dismiss(id) {
if (!id) {
toast.dismiss();
return Promise.resolve();
}
return new Promise((resolve) => {
if (toast.isActive(id)) {
toast.update(id, {
onClose: () => {
resolve();
},
});
toast.dismiss(id);
} else {
resolve();
}
});
}
@nelsoneldoro
Copy link
Author

Tested on [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment