@react-hookz/web
Version:
React hooks done right, for browser and SSR.
16 lines (15 loc) • 707 B
JavaScript
import { useSafeState, useThrottledCallback } from '..';
/**
* Like `useSafeState` but its state setter is throttled.
*
* @param initialState Initial state to pass to underlying `useSafeState`.
* @param delay Throttle delay.
* @param noTrailing If `noTrailing` is true, callback will only execute every
* `delay` milliseconds, otherwise, callback will be executed one final time
* after the last throttled-function call.
*/
export function useThrottledState(initialState, delay, noTrailing) {
if (noTrailing === void 0) { noTrailing = false; }
var _a = useSafeState(initialState), state = _a[0], setState = _a[1];
return [state, useThrottledCallback(setState, [], delay, noTrailing)];
}