For one of my current projects, I created a timer until the release on New Year's Eve - December 31, 2022. Since the project is written in React, I...
For further actions, you may consider blocking this person and/or reporting abuse
Nice beginner article @yuridevat and great comment @lukeshiru . I also would rather store the unix timespan than the values individually, it makes easier to do math with it and you wouldn't need even to
setTime
a new timespan every second, a simple subtraction would do the trick. =DBuilding on that, here's a handy hook:
Then its simple to use:
Yes good point! But as far I remember it deviates very irregularly in the milliseconds, so I tend to ignore it unless it's crucial π . But thanks for the video link, I'll watch it to refresh my memory on the subject π.
In that case only one
useEffect
is needed:Wow! Thank you for your suggestion! Since my tutorial is a beginner tutorial, I won't change it, but will include your comment in the article, because it could be very important for those who want to write the code in a professional way and this comment must not be overlooked in any case!
I myself will study your solution to fully understand it. It's time for me to finally write code in a professional way! You really made my day, thanks again!
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.
The explanation is beginner friendly. Kudos
Thanks, Samuel.
For the HTML, you could put a Β« timer Β» role and an attribute tabindex="0" on the container of the timer.
Shame on me. Thank you very much for this advice, Francois. I should indeed include accessible code in my (future) tutorials from the beginning, and not only for my accessibility articles themselves. I will do my research and implement it. I will keep you posted when I have revised the code.
Thank you for supporting me in getting better at accessibility every day.
Hi, you're missing a closing quote everywhere you mention
<div className="timer>
:)Copy paste π. Very attentive, thank you!
Hey Kaio! Could you please remove the link in your comment since this is not the right place to post it, thanks!
Nice peace. Many thanks.