UNPKG

@reservoir0x/relay-kit-ui

Version:

Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.

33 lines 1.44 kB
import { useMemo, useRef, useState } from 'react'; import { useDebounceValue, useDebounceCallback } from 'usehooks-ts'; export default function useDebounceState(initialValue, delay, options) { const memoOptions = useMemo(() => { return options; }, [options]); const eq = memoOptions?.equalityFn ?? ((left, right) => left === right); const unwrappedInitialValue = initialValue instanceof Function ? initialValue() : initialValue; const [debouncedValue, setDebouncedValue] = useState(unwrappedInitialValue); const [value, setValue] = useState(unwrappedInitialValue); const previousValueRef = useRef(unwrappedInitialValue); const updateDebouncedValue = useDebounceCallback(setDebouncedValue, delay, memoOptions); // Update the debounced value if the initial value changes if (!eq(previousValueRef.current, unwrappedInitialValue)) { updateDebouncedValue(unwrappedInitialValue); previousValueRef.current = unwrappedInitialValue; } return { value, debouncedValue, setValue: (value) => { updateDebouncedValue.cancel(); setValue(value); updateDebouncedValue(value); }, setDebouncedValue: (value) => { updateDebouncedValue.cancel(); updateDebouncedValue(value); }, debouncedControls: updateDebouncedValue }; } //# sourceMappingURL=useDebounceState.js.map