Create a React component that implements a timer. The timer should start when the component mounts, update every second, and stop updating when the component is unmounted. The goal is to practice how to use useEffect for side effects like setting up intervals and cleaning them up.
import React, { useState, useEffect } from 'react';
function Timer() {
const [seconds, setSeconds] = useState(0);
useEffect(() => {
// Set up the timer logic here.
return () => {
// Clean up the timer here when the component unmounts.
};
}, []); // Add appropriate dependencies.
return (
<div>
<h1>Timer: {seconds} seconds</h1>
</div>
);
}
export default Timer
useEffect hook to update the seconds state every second.useEffect to avoid memory leaks when the component unmounts.