Skip to content

Commit

Permalink
fix: updated useStableCallback to set callback in ref without useEffe…
Browse files Browse the repository at this point in the history
…ct (#2010)(by @pavel-krasnov)
  • Loading branch information
pavel-krasnov authored Nov 17, 2024
1 parent 398e711 commit e898859
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/hooks/useStableCallback.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useRef } from 'react';
import { useCallback, useRef } from 'react';

// biome-ignore lint: to be addressed!
type Callback = (...args: any[]) => any;
Expand All @@ -8,16 +8,11 @@ type Callback = (...args: any[]) => any;
*/
export const useStableCallback = (callback: Callback) => {
const callbackRef = useRef<Callback>();
callbackRef.current = callback;
const memoCallback = useCallback(
// biome-ignore lint: to be addressed!
(...args: any) => callbackRef.current && callbackRef.current(...args),
[]
);
useEffect(() => {
callbackRef.current = callback;
return () => {
callbackRef.current = undefined;
};
});
return memoCallback;
};

0 comments on commit e898859

Please sign in to comment.