mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-16 01:29:11 +08:00
28 lines
610 B
TypeScript
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];
|
||
|
}
|