Search code examples
reactjscountdown

is there any way to prevent counter from restarting


I'm trying to build a counter with two button start and stop, everything works great but when i want to add a function to change state on the (onClick) the counter restart every time the state changes.

code enter image description here


Solution

  • I think it's because rerenders <Countdown /> component every time setState happens. To fix this

    const date = React.useMemo(()=> {return (Date.now() + 1200000)}, []);
    ...
    <CountDown
    ...
    data={date}
    ...
    />