mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-15 13:43:39 +01:00
13 lines
465 B
TypeScript
13 lines
465 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 = 500,
|
|
): [T, Dispatch<SetStateAction<T>>, Dispatch<SetStateAction<T>>] {
|
|
const [state, setState] = useState<T>(defaultValue);
|
|
const debouncedSetState = useMemo(() => debounce(setState, delay), [delay]);
|
|
return [state, debouncedSetState, setState];
|
|
}
|