Skip to content

OpenStock is an open-source alternative to expensive market platforms. Track real-time prices, set personalized alerts, and explore detailed company insights — built openly, for everyone, forever free.

Update hooks/useDebounce.ts

Mr. Algorithm502b52e4f066
diff --git a/hooks/useDebounce.ts b/hooks/useDebounce.ts
index 3a6b138..2ffbe2f 100644
--- a/hooks/useDebounce.ts
+++ b/hooks/useDebounce.ts
@@ -4,12 +4,16 @@ import { useCallback, useRef } from 'react';
export function useDebounce(callback: () => void, delay: number) {
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
+ const callbackRef = useRef(callback);
+
+ // Keep callback ref up to date
+ callbackRef.current = callback;
return useCallback(() => {
- if(timeoutRef.current) {
+ if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
- timeoutRef.current = setTimeout(callback, delay);
- }, [callback, delay])
+ timeoutRef.current = setTimeout(() => callbackRef.current(), delay);
+ }, [delay])
}
\ No newline at end of file