1
0
mirror of https://github.com/vercel/commerce.git synced 2025-07-29 13:11:22 +00:00
Files
.github
.vscode
app
components
fonts
hooks
index.tsx
lib
public
.env.example
.eslintrc.js
.gitignore
.nvmrc
.prettierignore
README.md
license.md
next.config.js
package.json
pnpm-lock.yaml
postcss.config.js
prettier.config.js
tailwind.config.js
tsconfig.json
commerce/hooks/index.tsx
2024-05-08 08:38:44 +07:00

17 lines
446 B
TypeScript

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;
};