mirror of
https://github.com/mountain-loop/yaak.git
synced 2025-12-23 22:48:55 -05:00
13 lines
471 B
TypeScript
13 lines
471 B
TypeScript
import { debounce } from '@yaakapp-internal/lib';
|
|
import type { Dispatch, SetStateAction } from 'react';
|
|
import { useMemo, useState } from 'react';
|
|
|
|
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];
|
|
}
|