Skip to content

Commit

Permalink
Update use-memoized-callback.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
littensy committed Apr 22, 2023
1 parent f9ae4cc commit 6a19d27
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/use-memoized-callback/use-memoized-callback.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { useCallback, useMutable } from "@rbxts/roact-hooked";

export function useMemoizedCallback<T extends Callback>(callback: T): T;
export function useMemoizedCallback(callback: Callback): Callback {
/**
* Returns a memoized callback. When passed a new callback, the memoized
* callback will not change, but calling it will invoke the new callback.
* @param callback The callback to memoize.
* @returns The memoized callback.
*/
export function useMemoizedCallback<T extends Callback>(callback: T): T {
const callbackRef = useMutable(callback);
callbackRef.current = callback;

return useCallback((...args) => {
return useCallback((...args: unknown[]) => {
return callbackRef.current(...args);
}, []);
}, []) as T;
}

0 comments on commit 6a19d27

Please sign in to comment.