Skip to content

Commit

Permalink
fix: hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
linjinze999 committed May 11, 2024
1 parent 9487d76 commit 134a81d
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/hippy_ui_react/src/utils/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useRef } from 'react';
import { Platform } from '@hippy/react';

/**
Expand Down Expand Up @@ -39,7 +40,23 @@ export class Throttle<T extends any[]> {
}
};
}
/**
* 工具:节流-hook
* */
export function useThrottle<T extends any[]>(fn: (...args: T) => void, delay = 300, atBegin = true) {
const { current } = useRef(new Throttle(fn, delay, atBegin));

useEffect(() => {
current.replaceFn(fn);
}, [fn]);

useEffect(() => {
return () => {
current.cancel();
};
}, []);
return current;
}
/**
* 工具:防抖
* */
Expand Down Expand Up @@ -81,9 +98,24 @@ export class Debounce<T extends any[]> {
}
};
}
/**
* 工具:防抖-hook
* */
export function useDebounce<T extends any[]>(fn: (...args: T) => void, delay = 300, atBegin = false) {
const { current } = useRef(new Debounce(fn, delay, atBegin));
useEffect(() => {
current.replaceFn(fn);
}, [fn]);
useEffect(() => {
return () => {
current.cancel();
};
}, []);
return current;
}

/**
* 工具:版本比较
* 工具:版本比较(返回a <= b)
* */
export function versionCompare(target: string, current: string) {
const targetArr = target.split('.');
Expand Down

0 comments on commit 134a81d

Please sign in to comment.