docs: fix cleanup in adding interactivity example (#333)

The previous solution did not clear interval after the time has passed.
The "now" variable was not being updated in useEffect().
This commit is contained in:
umfy
2022-07-04 10:56:14 +00:00
committed by GitHub
parent cbbcdc7d93
commit 1ac2166daa
+6 -4
View File
@@ -46,10 +46,12 @@ export default function Countdown(props: { target: string }) {
// date as long as the component is mounted.
useEffect(() => {
const timer = setInterval(() => {
setNow(new Date());
if (now > target) {
clearInterval(timer);
}
setNow((now) => {
if (now > target) {
clearInterval(timer);
}
return new Date();
});
}, 1000);
return () => clearInterval(timer);
}, [props.target]);