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:
@@ -46,10 +46,12 @@ export default function Countdown(props: { target: string }) {
|
|||||||
// date as long as the component is mounted.
|
// date as long as the component is mounted.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timer = setInterval(() => {
|
const timer = setInterval(() => {
|
||||||
setNow(new Date());
|
setNow((now) => {
|
||||||
if (now > target) {
|
if (now > target) {
|
||||||
clearInterval(timer);
|
clearInterval(timer);
|
||||||
}
|
}
|
||||||
|
return new Date();
|
||||||
|
});
|
||||||
}, 1000);
|
}, 1000);
|
||||||
return () => clearInterval(timer);
|
return () => clearInterval(timer);
|
||||||
}, [props.target]);
|
}, [props.target]);
|
||||||
|
|||||||
Reference in New Issue
Block a user