Skip to content

An opinionated toast component for React.

fix flush sync error (#586)

Emil Kowalski1829bfa7af83
diff --git a/src/index.tsx b/src/index.tsx
index 6c66c89..585903d 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -649,20 +649,22 @@ const Toaster = React.forwardRef<HTMLElement, ToasterProps>(function Toaster(pro
}
// Prevent batching, temp solution.
- ReactDOM.flushSync(() => {
- setToasts((toasts) => {
- const indexOfExistingToast = toasts.findIndex((t) => t.id === toast.id);
-
- // Update the toast if it already exists
- if (indexOfExistingToast !== -1) {
- return [
- ...toasts.slice(0, indexOfExistingToast),
- { ...toasts[indexOfExistingToast], ...toast },
- ...toasts.slice(indexOfExistingToast + 1),
- ];
- }
+ setTimeout(() => {
+ ReactDOM.flushSync(() => {
+ setToasts((toasts) => {
+ const indexOfExistingToast = toasts.findIndex((t) => t.id === toast.id);
- return [toast, ...toasts];
+ // Update the toast if it already exists
+ if (indexOfExistingToast !== -1) {
+ return [
+ ...toasts.slice(0, indexOfExistingToast),
+ { ...toasts[indexOfExistingToast], ...toast },
+ ...toasts.slice(indexOfExistingToast + 1),
+ ];
+ }
+
+ return [toast, ...toasts];
+ });
});
});
});