mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-27 20:49:53 +08:00
502dac12aa
* docs: fix code * feat: lint * feat: prettier * feat: test * feat: review * feat: format html * feat: format html
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import * as React from 'react';
|
|
import { useRef } from 'react';
|
|
import raf from 'rc-util/lib/raf';
|
|
|
|
type Updater<ValueType> = (prev?: ValueType) => ValueType;
|
|
|
|
export default function useFrameState<ValueType>(
|
|
defaultValue: ValueType,
|
|
): [ValueType, (updater: Updater<ValueType>) => void] {
|
|
const [value, setValue] = React.useState(defaultValue);
|
|
const frameRef = useRef<number | null>(null);
|
|
const batchRef = useRef<Updater<ValueType>[]>([]);
|
|
const destroyRef = useRef(false);
|
|
|
|
React.useEffect(() => {
|
|
destroyRef.current = false;
|
|
return () => {
|
|
destroyRef.current = true;
|
|
raf.cancel(frameRef.current!);
|
|
frameRef.current = null;
|
|
};
|
|
}, []);
|
|
|
|
function setFrameValue(updater: Updater<ValueType>) {
|
|
if (destroyRef.current) {
|
|
return;
|
|
}
|
|
|
|
if (frameRef.current === null) {
|
|
batchRef.current = [];
|
|
frameRef.current = raf(() => {
|
|
frameRef.current = null;
|
|
setValue((prevValue) => {
|
|
let current = prevValue;
|
|
|
|
batchRef.current.forEach((func) => {
|
|
current = func(current);
|
|
});
|
|
|
|
return current;
|
|
});
|
|
});
|
|
}
|
|
|
|
batchRef.current.push(updater);
|
|
}
|
|
|
|
return [value, setFrameValue];
|
|
}
|