From 13f612e18bf132cfeb36e08f70700f632eeb1d81 Mon Sep 17 00:00:00 2001 From: Emil Kowalski Date: Thu, 20 Feb 2025 12:14:33 +0100 Subject: [PATCH] fix flush sync error --- src/index.tsx | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) 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(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]; + }); }); }); });