@react-hookz/web
Version:
React hooks done right, for browser and SSR.
15 lines (14 loc) • 623 B
JavaScript
import { useDebouncedCallback, useSafeState } from '..';
/**
* Like `useSafeState` but its state setter is debounced.
*
* @param initialState Initial state to pass to underlying `useSafeState`.
* @param delay Debounce delay.
* @param maxWait Maximum amount of milliseconds that function can be delayed
* before it's force execution. 0 means no max wait.
*/
export function useDebouncedState(initialState, delay, maxWait) {
if (maxWait === void 0) { maxWait = 0; }
var _a = useSafeState(initialState), state = _a[0], setState = _a[1];
return [state, useDebouncedCallback(setState, [], delay, maxWait)];
}