Skip to content

An opinionated toast component for React.

Fix toast.dismiss without an id (#609)

Emil Kowalski0ae333b3a95e
diff --git a/src/index.tsx b/src/index.tsx
index e8ddbc6..8d79b92 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -640,10 +640,9 @@ const Toaster = React.forwardRef<HTMLElement, ToasterProps>(function Toaster(pro
React.useEffect(() => {
return ToastState.subscribe((toast) => {
if ((toast as ToastToDismiss).dismiss) {
- const map = toasts.map((t) => (t.id === toast.id ? { ...t, delete: true } : t));
// Prevent batching of other state updates
requestAnimationFrame(() => {
- setToasts(map);
+ setToasts((toasts) => toasts.map((t) => (t.id === toast.id ? { ...t, delete: true } : t)));
});
return;
}
diff --git a/src/state.ts b/src/state.ts
index 51285ff..23aa371 100644
--- a/src/state.ts
+++ b/src/state.ts
@@ -86,15 +86,15 @@ class Observer {
};
dismiss = (id?: number | string) => {
- this.dismissedToasts.add(id);
-
- if (!id) {
+ if (id) {
+ this.dismissedToasts.add(id);
+ requestAnimationFrame(() => this.subscribers.forEach((subscriber) => subscriber({ id, dismiss: true })));
+ } else {
this.toasts.forEach((toast) => {
this.subscribers.forEach((subscriber) => subscriber({ id: toast.id, dismiss: true }));
});
}
- requestAnimationFrame(() => this.subscribers.forEach((subscriber) => subscriber({ id, dismiss: true })));
return id;
};