2024-04-22 10:26:14 +08:00
|
|
|
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);
|
|
|
|
|
2024-12-18 14:09:49 +08:00
|
|
|
const rafRef = React.useRef<number>(null);
|
2024-04-22 10:26:14 +08:00
|
|
|
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];
|
|
|
|
}
|