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...)
👋 Selftaught Accessibility & Frontend Developer
💻 Dev & UX Accessibility Specialist @Atos | CPACC
🙆 Improving the world with accessible web content
✍️ Content creator, Tips on How To Get Into Tech
May I suggest a few things?
Date.now()
would do the trick instead ofDate.parse(new Date())
.let interval
could beconst interval
. It's never reassigned.deadline
within the scope of Timer there is no need to use it as a parameter for thegetTime
function.timeDiff
and 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
getTime
function 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.