Skip to content

Commit

Permalink
fix: 🐛 useDebounceCallback/useThrottleCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoshuangLi committed Nov 16, 2023
1 parent cd39bdf commit 005fae2
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Component/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,31 @@ export const useEventCallback = (fn) => {
}, [ref]);
};
const useEventArgument = (arg) => {
const keys = Object.keys(arg);
const values = Object.values(arg);
const objective = typeof arg === 'object';
const dependencies = objective ? [...keys, ...values] : [arg];
return useMemo(() => arg, dependencies);
};
const useEventArguments = (...args) => args.map(useEventArgument);
export const useDebounceCallback = (callback, ...args) => {
const fn = useEventCallback(callback);
args = useEventArguments(...args);
callback = useEventCallback(callback);
callback = useMemo(() => debounce(callback, ...args), args);
return useCallback(debounce(fn, ...args), [fn]);
return useCallback(callback, [callback]);
};
export const useThrottleCallback = (callback, ...args) => {
const fn = useEventCallback(callback);
args = useEventArguments(...args);
callback = useEventCallback(callback);
callback = useMemo(() => throttle(callback, ...args), args);
return useCallback(throttle(fn, ...args), [fn]);
return useCallback(callback, [callback]);
};
`;

0 comments on commit 005fae2

Please sign in to comment.