Files
yaak/src-web/hooks/useDebouncedState.ts
2024-01-18 20:40:56 -08:00

13 lines
468 B
TypeScript

import type { Dispatch, SetStateAction } from 'react';
import { useMemo, useState } from 'react';
import { debounce } from '../lib/debounce';
export function useDebouncedState<T>(
defaultValue: T,
delay?: number,
): [T, Dispatch<SetStateAction<T>>, Dispatch<SetStateAction<T>>] {
const [state, setState] = useState<T>(defaultValue);
const debouncedSetState = useMemo(() => debounce(setState, delay), [delay]);
return [state, debouncedSetState, setState];
}