Skip to content

Commit

Permalink
feat: add usePersistCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerleung0411 authored and yinkaihui committed Jun 11, 2024
1 parent e074110 commit 4295b2e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions components/_util/hooks/usePersistCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useRef, useCallback } from 'react';

export default function usePersistCallback<T extends (...args: any[]) => any>(fn: T) {
const ref = useRef<T>();

ref.current = fn;

return useCallback<T>(
// @ts-ignore
(...args) => {
const fn = ref.current;
return fn && fn(...args);
},
[ref]
);
}

0 comments on commit 4295b2e

Please sign in to comment.