How do you create a stopwatch with Next.js?

Question: I tried to create a stopwatch on Next.js, but it keeps bugging out whenever I do. For some weird reason, it keeps running through all of the previous values before rendering the desired output. Can somebody please help me with my issue?

Repl link: Stopwatch Test

Here is the code for the first page:

import { useState } from 'react';

export default function test() {
    const [currentTime, setTime] = useState(0);
    setInterval(() => {
        setTime(currentTime + 1);
    }, 1000);
    return <h1>{currentTime}</h1>
}

Never mind, fixed it. I just needed a useEffect to clear the previous values.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.