Skip to content

Commit 1e0e121

Browse files
committed
fix memory leak by putting setInterval() in a useeffect
1 parent 22c24f5 commit 1e0e121

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

app/wall/components/ShowHideBeamInfo.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import Image from "next/image";
2-
import { useState } from "react";
2+
import { useEffect, useState } from "react";
33

44
export default function ShowHideBeamInfo() {
5-
const [date, setDate] = useState<number>(Date.now());
6-
7-
setInterval(() => {
8-
// Update the date, used by the beam image to get a fresh image, every 5 seconds so we're not constantly reloading the image on every render.
9-
setDate(Date.now());
10-
}, 5000);
5+
const [date, setDate] = useState<number>(0);
6+
useEffect(() => {
7+
const interval = setInterval(() => {
8+
// Update the date, used by the beam image to get a fresh image, every 15 seconds so we're not constantly reloading the image on every render.
9+
setDate(Date.now());
10+
}, 15000);
11+
return () => clearInterval(interval);
12+
}, [date]);
1113

1214
return (
1315
<div id="beampic" className="flex flex-col items-center justify-center">
@@ -23,7 +25,6 @@ export default function ShowHideBeamInfo() {
2325
alt="beam info"
2426
height={600}
2527
width={600}
26-
suppressHydrationWarning // needed to avoid the server-side rendering hydration warning because the "date" variable will be further ahead on the client than the server.
2728
/>
2829
</span>
2930
</label>

0 commit comments

Comments
 (0)