Skip to content

Commit 0b15561

Browse files
fix: Recalculate time ago on re-render (#4107)
1 parent 2443d73 commit 0b15561

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

packages/common/src/hooks.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ const calculateTimeAgo = (timestamp: Date) => {
1111
};
1212

1313
export const useTimeAgo = (timestamp: Date, updateFrequency = 1000) => {
14-
const [timeAgo, setTimeAgo] = useState(calculateTimeAgo(timestamp));
14+
const [timeAgo, setTimeAgo] = useState(() => calculateTimeAgo(timestamp));
15+
16+
useEffect(() => {
17+
setTimeAgo(calculateTimeAgo(timestamp));
18+
}, [timestamp]);
1519

1620
useEffect(() => {
1721
const intervalId = setInterval(() => setTimeAgo(calculateTimeAgo(timestamp)), updateFrequency);
1822

1923
return () => clearInterval(intervalId); // clear interval on hook unmount
20-
}, [timestamp]);
24+
}, [timestamp, updateFrequency]);
2125

2226
return timeAgo;
2327
};

0 commit comments

Comments
 (0)