Skip to content

Commit 1556d28

Browse files
timolinsjluxenberg
andcommitted
Fix race condition in backwards compatible way
Co-Authored-By: Jared Luxenberg <jared@jaredlux.com>
1 parent a9c12f1 commit 1556d28

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/core/store.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useEffect, useState, useRef } from 'react';
22
import { DefaultToastOptions, Toast, ToastType } from './types';
33

44
const TOAST_LIMIT = 20;
@@ -143,15 +143,21 @@ export const defaultTimeouts: {
143143

144144
export const useStore = (toastOptions: DefaultToastOptions = {}): State => {
145145
const [state, setState] = useState<State>(memoryState);
146+
const initial = useRef(memoryState);
147+
148+
// TODO: Switch to useSyncExternalStore when targeting React 18+
146149
useEffect(() => {
150+
if (initial.current !== memoryState) {
151+
setState(memoryState);
152+
}
147153
listeners.push(setState);
148154
return () => {
149155
const index = listeners.indexOf(setState);
150156
if (index > -1) {
151157
listeners.splice(index, 1);
152158
}
153159
};
154-
}, [state]);
160+
}, []);
155161

156162
const mergedToasts = state.toasts.map((t) => ({
157163
...toastOptions,

0 commit comments

Comments
 (0)