Junior — Senior
React component displaying the current time
livecode
Task condition
Create a React component that displays the current date and time and updates it every second. When the component unmounts, it should call the logMetric function, passing the last displayed time.
function logMetric(date: string) {
fetch('/api/metric', date);
}
const Clock = () => {
const [currentDate, setCurrentDate] = useState((new Date()).toISOString());
return <h1>{currentDate}</h1>;
};
To implement this, you'll need to use the useEffect hook: create a timer inside it that calls setCurrentDate every second, and in the cleanup function, call logMetric with the current date value.