You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
portainer/app/react/hooks/useDebounce.ts

23 lines
606 B

import _ from 'lodash';
import { useState, useRef, useCallback, useEffect } from 'react';
export function useDebounce(value: string, onChange: (value: string) => void) {
const [debouncedValue, setDebouncedValue] = useState(value);
const onChangeDebounces = useRef(_.debounce(onChange, 300));
const handleChange = useCallback(
(value: string) => {
setDebouncedValue(value);
onChangeDebounces.current(value);
},
[onChangeDebounces, setDebouncedValue]
);
useEffect(() => {
setDebouncedValue(value);
}, [value]);
return [debouncedValue, handleChange] as const;
}