ant-design/components/slider/useRafLock.ts
二货爱吃白萝卜 c321492eeb
fix: Slider range exchange tooltip blink (#48536)
* fix: slider tooltip pos

* chore: clean up

* chore: fix style

* chore: bump rc-slider

* fix: lock clean up logic

* chore: finish

* test: update snapshot

* chore: bump rc-slider

* test: update snapshot

* test: test coverage

* chore: clean up
2024-04-22 10:26:14 +08:00

28 lines
610 B
TypeScript

import * as React from 'react';
import raf from 'rc-util/lib/raf';
export default function useRafLock(): [state: boolean, setState: (nextState: boolean) => void] {
const [state, setState] = React.useState(false);
const rafRef = React.useRef<number>();
const cleanup = () => {
raf.cancel(rafRef.current!);
};
const setDelayState = (nextState: boolean) => {
cleanup();
if (nextState) {
setState(nextState);
} else {
rafRef.current = raf(() => {
setState(nextState);
});
}
};
React.useEffect(() => cleanup, []);
return [state, setDelayState];
}