We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2443d73 commit 0b15561Copy full SHA for 0b15561
packages/common/src/hooks.ts
@@ -11,13 +11,17 @@ const calculateTimeAgo = (timestamp: Date) => {
11
};
12
13
export const useTimeAgo = (timestamp: Date, updateFrequency = 1000) => {
14
- const [timeAgo, setTimeAgo] = useState(calculateTimeAgo(timestamp));
+ const [timeAgo, setTimeAgo] = useState(() => calculateTimeAgo(timestamp));
15
+
16
+ useEffect(() => {
17
+ setTimeAgo(calculateTimeAgo(timestamp));
18
+ }, [timestamp]);
19
20
useEffect(() => {
21
const intervalId = setInterval(() => setTimeAgo(calculateTimeAgo(timestamp)), updateFrequency);
22
23
return () => clearInterval(intervalId); // clear interval on hook unmount
- }, [timestamp]);
24
+ }, [timestamp, updateFrequency]);
25
26
return timeAgo;
27
0 commit comments