Initial project bootstrap
这个提交包含在:
20
client/src/hooks/usePersistFn.ts
普通文件
20
client/src/hooks/usePersistFn.ts
普通文件
@@ -0,0 +1,20 @@
|
||||
import { useRef } from "react";
|
||||
|
||||
type noop = (...args: any[]) => any;
|
||||
|
||||
/**
|
||||
* usePersistFn instead of useCallback to reduce cognitive load
|
||||
*/
|
||||
export function usePersistFn<T extends noop>(fn: T) {
|
||||
const fnRef = useRef<T>(fn);
|
||||
fnRef.current = fn;
|
||||
|
||||
const persistFn = useRef<T>(null);
|
||||
if (!persistFn.current) {
|
||||
persistFn.current = function (this: unknown, ...args) {
|
||||
return fnRef.current!.apply(this, args);
|
||||
} as T;
|
||||
}
|
||||
|
||||
return persistFn.current!;
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户