2022-12-16 02:06:14 +00:00
|
|
|
import { useState } from 'react';
|
|
|
|
import { debounce } from 'debounce';
|
|
|
|
|
|
|
|
type DebounceFn<V> = ((value: V) => void) & { clear: () => void };
|
|
|
|
|
2023-01-12 19:31:47 +00:00
|
|
|
export default <S>(initial: S, interval?: number, immediate?: boolean): [S, DebounceFn<S>] => {
|
|
|
|
const [state, setState] = useState<S>(initial);
|
2022-12-16 02:06:14 +00:00
|
|
|
|
|
|
|
const debouncedSetState = debounce((v: S) => setState(v), interval, immediate);
|
|
|
|
|
2023-01-12 19:31:47 +00:00
|
|
|
return [state, debouncedSetState];
|
2022-12-16 02:06:14 +00:00
|
|
|
};
|