mirror of
https://github.com/vercel/commerce.git
synced 2025-09-07 16:30:17 +00:00
16
hooks/use-debounce.tsx
Normal file
16
hooks/use-debounce.tsx
Normal file
@@ -0,0 +1,16 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
export const useDebounce = (value: string, delay = 500) => {
|
||||
const [debouncedValue, setDebouncedValue] = useState('');
|
||||
const timerRef = useRef<ReturnType<typeof setTimeout>>();
|
||||
|
||||
useEffect(() => {
|
||||
timerRef.current = setTimeout(() => setDebouncedValue(value), delay);
|
||||
|
||||
return () => {
|
||||
clearTimeout(timerRef.current);
|
||||
};
|
||||
}, [value, delay]);
|
||||
|
||||
return debouncedValue;
|
||||
};
|
Reference in New Issue
Block a user