GoScrobble/web/node_modules/@restart/hooks/esm/useDebouncedState.js

24 lines
679 B
JavaScript
Raw Normal View History

2022-04-25 02:47:15 +00:00
import { useState } from 'react';
import useDebouncedCallback from './useDebouncedCallback';
/**
* Similar to `useState`, except the setter function is debounced by
* the specified delay.
*
* ```ts
* const [value, setValue] = useDebouncedState('test', 500)
*
* setValue('test2')
* ```
*
* @param initialState initial state value
* @param delay The milliseconds delay before a new value is set
*/
export default function useDebouncedState(initialState, delay) {
var _useState = useState(initialState),
state = _useState[0],
setState = _useState[1];
var debouncedSetState = useDebouncedCallback(setState, delay);
return [state, debouncedSetState];
}