Skip to content

Commit e549471

Browse files
committed
refactor: useSyncExternalStore useStore refactor
1 parent 1f70053 commit e549471

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/core/store.ts

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

44
const TOAST_LIMIT = 20;
@@ -179,16 +179,19 @@ export const defaultTimeouts: {
179179
};
180180

181181
export const useStore = (toastOptions: DefaultToastOptions = {}): State => {
182-
const [state, setState] = useState<State>(memoryState);
183-
useEffect(() => {
184-
listeners.push(setState);
185-
return () => {
186-
const index = listeners.indexOf(setState);
187-
if (index > -1) {
188-
listeners.splice(index, 1);
189-
}
190-
};
191-
}, [state]);
182+
const state = useSyncExternalStore(
183+
(callback) => {
184+
listeners.push(callback);
185+
return () => {
186+
const index = listeners.indexOf(callback);
187+
if (index > -1) {
188+
listeners.splice(index, 1);
189+
}
190+
};
191+
},
192+
() => memoryState,
193+
() => memoryState
194+
);
192195

193196
const mergedToasts = state.toasts.map((t) => ({
194197
...toastOptions,

0 commit comments

Comments
 (0)