@react-md/utils
Version:
General utils for react-md.
31 lines • 1.24 kB
JavaScript
import { useCallback, useRef } from "react";
/**
* Creates a temporary value that gets reset every `x`ms back to the provided
* default value. This is useful when doing keyboard searching or other
* interactions.
*
* NOTE: This does not force a re-render when the value changes and instead uses
* a ref value instead.
*
* @typeParam T - the type for the value
* @param defaultValue - The default value to use. Each time the reset timeout
* is triggered, this value will be set again.
* @param resetTime - The amount of time before the value is reset back to the
* default value
*/
export function useTempValue(defaultValue, resetTime) {
if (resetTime === void 0) { resetTime = 500; }
var value = useRef(defaultValue);
var timeout = useRef();
var resetValue = useCallback(function () {
window.clearTimeout(timeout.current);
value.current = defaultValue;
}, [defaultValue]);
var setValue = useCallback(function (nextValue) {
value.current = nextValue;
window.clearTimeout(timeout.current);
timeout.current = window.setTimeout(resetValue, resetTime);
}, [resetTime, resetValue]);
return [value, setValue, resetValue];
}
//# sourceMappingURL=useTempValue.js.map