Last active
September 26, 2024 04:30
-
-
Save nelsoneldoro/306aef4a975830868a47707fa30470f9 to your computer and use it in GitHub Desktop.
react-toastify: dismiss as promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
</> | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on
[email protected]