Files
ant-design/components/slider/useRafLock.ts
lijianan ab0e07e25d refactor: [v6] use rc-component (#52337)
* refactor: use @rc-component

* chore: adjust compile

* test: fix logic

* chore: back of reset

---------

Co-authored-by: 二货机器人 <smith3816@gmail.com>
2025-01-10 14:14:31 +08:00

28 lines
625 B
TypeScript

import * as React from 'react';
import raf from '@rc-component/util/lib/raf';
export default function useRafLock(): [state: boolean, setState: (nextState: boolean) => void] {
const [state, setState] = React.useState(false);
const rafRef = React.useRef<number>(null);
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];
}