With React 18's Automatic Batching this is no longer that important, but I feel it's better to have less different state variables if they are always dependent.
Also this approach would make it easier to have really pure getTime function which only returns the object used to set the new state.
To repeat this function, we must clear the interval after each rendering using the clearInterval() function.
This is not entirely true: the returned function in useEffecxt is only called when the component is unmounted (or on changes in the deps, but since there are none in this case...)
I write bit-sized stories about the internet.
Since 2026: Python Developer, into Cyber Security (p0hnk3m0n@TryHackMe)
2020-2025: Accessibility Specialist
May I suggest a few things?
Date.now()would do the trick instead ofDate.parse(new Date()).let intervalcould beconst interval. It's never reassigned.deadlinewithin the scope of Timer there is no need to use it as a parameter for thegetTimefunction.timeDiffand use an object in the stateWith React 18's Automatic Batching this is no longer that important, but I feel it's better to have less different state variables if they are always dependent.
Also this approach would make it easier to have really pure
getTimefunction which only returns the object used to set the new state.This is not entirely true: the returned function in useEffecxt is only called when the component is unmounted (or on changes in the deps, but since there are none in this case...)
Thank you so much for your suggestions, I will insert most of your suggestions (the ones I get 😅) right away!
Doh', I thought that I did not fully understand setInterval/clearInterval and useEffect. I will read about it again and update the information.
Thank you for helping me making this tutorial better, I really appreciate it.
If you have any questions feel free to reach out.