mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-15 08:13:27 +01:00
13 lines
432 B
TypeScript
13 lines
432 B
TypeScript
import type { Dispatch, SetStateAction } from 'react';
|
|
import { useMemo, useState } from 'react';
|
|
import { debounce } from '../lib/debounce';
|
|
|
|
export function useDebouncedSetState<T>(
|
|
defaultValue: T,
|
|
delay?: number,
|
|
): [T, Dispatch<SetStateAction<T>>] {
|
|
const [state, setState] = useState<T>(defaultValue);
|
|
const debouncedSetState = useMemo(() => debounce(setState, delay), [delay]);
|
|
return [state, debouncedSetState];
|
|
}
|