Skip to content

Commit

Permalink
fix flush sync error (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilkowalski authored Feb 20, 2025
1 parent d66388d commit 1829bfa
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
});
});
});
Expand Down

0 comments on commit 1829bfa

Please sign in to comment.