mirror of https://github.com/usememos/memos
* fix the bug can't delete multiple files #1576 * using useEvent instead of useRef * delete unused code * delete unused code * change hook file name * refactor the useEvent * delete unnecessary export * fix import * Apply suggestions from code review --------- Co-authored-by: boojack <stevenlgtm@gmail.com>pull/1614/head
parent
4603f414db
commit
f7a1680f72
@ -0,0 +1,25 @@
|
|||||||
|
import React, { useEffect, useRef, EffectCallback, DependencyList } from "react";
|
||||||
|
|
||||||
|
const useIsoMorphicEffect = (effect: EffectCallback, deps?: DependencyList | undefined) => {
|
||||||
|
useEffect(effect, deps);
|
||||||
|
};
|
||||||
|
|
||||||
|
function useLatestValue<T>(value: T) {
|
||||||
|
const cache = useRef(value);
|
||||||
|
|
||||||
|
useIsoMorphicEffect(() => {
|
||||||
|
cache.current = value;
|
||||||
|
}, [value]);
|
||||||
|
|
||||||
|
return cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Add React.useEvent ?? once the useEvent hook is available
|
||||||
|
function useEvent<F extends (...args: any[]) => any, P extends any[] = Parameters<F>, R = ReturnType<F>>(
|
||||||
|
cb: (...args: P) => R
|
||||||
|
) {
|
||||||
|
const cache = useLatestValue(cb);
|
||||||
|
return React.useCallback((...args: P) => cache.current(...args), [cache]);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useEvent;
|
||||||
Loading…
Reference in New Issue