Skip to content

Commit e898859

Browse files
fix: updated useStableCallback to set callback in ref without useEffect (#2010)(by @pavel-krasnov)
1 parent 398e711 commit e898859

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

src/hooks/useStableCallback.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useRef } from 'react';
1+
import { useCallback, useRef } from 'react';
22

33
// biome-ignore lint: to be addressed!
44
type Callback = (...args: any[]) => any;
@@ -8,16 +8,11 @@ type Callback = (...args: any[]) => any;
88
*/
99
export const useStableCallback = (callback: Callback) => {
1010
const callbackRef = useRef<Callback>();
11+
callbackRef.current = callback;
1112
const memoCallback = useCallback(
1213
// biome-ignore lint: to be addressed!
1314
(...args: any) => callbackRef.current && callbackRef.current(...args),
1415
[]
1516
);
16-
useEffect(() => {
17-
callbackRef.current = callback;
18-
return () => {
19-
callbackRef.current = undefined;
20-
};
21-
});
2217
return memoCallback;
2318
};

0 commit comments

Comments
 (0)