Skip to content

Commit

Permalink
fix: useStableCallback implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed Nov 17, 2024
1 parent e7abc77 commit 87a73c5
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/hooks/useStableCallback.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { useCallback, useEffect, useRef } from 'react';
import { useCallback, useEffect, useLayoutEffect, useRef } from 'react';

// biome-ignore lint: to be addressed!
type Callback = (...args: any[]) => any;
type Callback<T> = (...args: T[]) => any;

/**
* Provide a stable version of useCallback
* https://gist.github.com/JakeCoxon/c7ebf6e6496f8468226fd36b596e1985
* Provide a stable version of useCallback.
*/
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),
[]
);
export function useStableCallback<T>(callback: Callback<T>) {
const callbackRef = useRef<Callback<T>>();

useLayoutEffect(() => {
callbackRef.current = callback;
});

useEffect(() => {
return () => {
callbackRef.current = undefined;
};
});
return memoCallback;
};
}, []);

return useCallback<Callback<T>>((...args) => {
return callbackRef.current?.(...args);
}, []);
}

0 comments on commit 87a73c5

Please sign in to comment.