React state has some limitations. That's where React references come in handy. In this tutorial, you will learn some common uses of the useRef React hook, and we will also build a React timer by persisting values with useRef. Just getting started with React? I recommend my 9 hour full course React tutorial here: kzbin.info/www/bejne/iIepcqx8jLRnndE
@csfundamental19772 жыл бұрын
You are million times better than those young punks teaching and making simpler things harder. I rarely likes the video but doing it for algorithm, you deserve more views sir. Keep posting good stuff.
@DaveGrayTeachesCode2 жыл бұрын
Thank you! I appreciate your comment 🙏💯
@bobbyboxer2664 Жыл бұрын
One of the clearest examples I've seen in a while. Thanks Dave 👍👍😁😁
@DaveGrayTeachesCode Жыл бұрын
Glad it was helpful!
@TruthSeeker23-m4o2 жыл бұрын
we are waiting for Typescript course dave. Many people are looking forward to it ReactwithTypescript. Also you are a Natural Teacher hats off god bless :)
@DaveGrayTeachesCode2 жыл бұрын
Thank you for the kind words! 💯
@ginogarcia87302 жыл бұрын
ah man! I hope this video gets shared more to make a timer or countdown in react, it's so simple compared to the others!!!!!!!!!!!!!!! i put alot of exclamation points because i finally found salvation haha and you explained things so simple and great. you even helped someone fix the plus clicks on start timer bug.
@DaveGrayTeachesCode2 жыл бұрын
Glad I could help!
@lix56947 ай бұрын
Thanks!! Very good examples.
@robdev892 жыл бұрын
Hey Dave! Just discovered your channel! I love your content, straight forward and easy to understand. Thanks.
@DaveGrayTeachesCode2 жыл бұрын
You're welcome! 💯
@md-abid-hussain Жыл бұрын
Thanks for this lesson Dave. Explanation was great. However there is a bug when we click start timer more than once the counter will increment more than once in a second and i doesn't stop even after clicking stop. And thanks to your past lesson I was able to solve this easily. Looking forward to learn more from you
@DaveGrayTeachesCode Жыл бұрын
You're welcome and concerning the bug you mention, I wasn't thinking about this as an app but rather just as an example. Sounds like you implemented the fix you wanted and that could be prevented in a couple of ways.
@allegy24142 жыл бұрын
Great video! I wanted to make simple page of two timers and this video made it a lot easier. thanks!
@DaveGrayTeachesCode2 жыл бұрын
Glad I could help! 💯
@ashvb10852 жыл бұрын
Amazing tut Dave! A very humble request! Please create a detailed tut on use of SocketIO with react and how we can handle realtime data updating in React. I looked up at some online tuts but am not able to understand their workarounds with useRef and useEffect while using the socket on client! Your sincere student!
@DaveGrayTeachesCode2 жыл бұрын
Thank you, Ash - and thank you for the request! 💯
@vincent35422 жыл бұрын
Hi Mr Dave, I really looking forward for your courses on typescript and react, Redux... You are amazing teacher 🙏❤️👍
@DaveGrayTeachesCode2 жыл бұрын
Thank you for the kind words, Vincent! 🙏🙏
@ChaiPark2 жыл бұрын
Thanks for the awesome tutorial, Dave! I really like the way you explain it and have followed several videos. Is there a certain reason that you used useRef() for 'timerId'? When I tried to use useState() for timerId, it also worked. Could you explain why you used useRef() for the timerId bit more?
@DaveGrayTeachesCode2 жыл бұрын
useRef provides the same object reference on every render. That is why it can be used to count renders, but useState will not do this. The renders.current++ you see is not possible otherwise. I hope this helps!
@siddiqahmed32743 жыл бұрын
That's cool.... I think you should put a full react project using hooks so we may learn how to put together a react application in a reactful way. Btw thank you for all great videos❤️
@DaveGrayTeachesCode3 жыл бұрын
Thank you Siddiq! 🙏 I will do that 💯🚀
@GabrielMartinez-ez9ue2 жыл бұрын
Dave, is there a way to contact you regarding a question? I am getting an undefined when setting an adding an event to useRef function. Maybe you can spot the bug.
@DaveGrayTeachesCode2 жыл бұрын
Hi Gabriel, I cannot take the time to debug all viewers code, but I do suggest you download the source code from the link provided in the description. From there, you can compare your code to mine to find the differences.
@ahmad-murery3 жыл бұрын
Just another great video , So, useRef() will return the same object on each render (each call to App function), What if we increment (renders) directly inside the App function and not inside any of declared handlers? this way we'll get the (renders) updated without worrying much about what triggers the rendering process, besides, it will count the initial render, Thanks Dave,
@DaveGrayTeachesCode3 жыл бұрын
As always, a great comment my friend! 💯 Edit: I wanted to add this will work if you are not using (check your index.js)... otherwise, StrictMode will double invoke render in an effort to detect side-effects with the render count doubling when only inside the App function and not inside a handler. More on this here: reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects 🚀
@ahmad-murery3 жыл бұрын
@@DaveGrayTeachesCode I really liked the way you're starting this series of videos recently by presenting a problem then explaining why and how to fix it ✔
@ghostintheshelf8660 Жыл бұрын
Thank you for a lesson, Dave! I'm a beginner in react and I don't get why do we need to set another useRef(timerId) for our setInterval function... Could someone explain why can't we just use regular variable to name our setInterval? thanks!
@sonamohialdin33762 жыл бұрын
So good tutorial about this hook
@DaveGrayTeachesCode2 жыл бұрын
Thank you, Sona! 💯
@fredylagartija Жыл бұрын
Hello Dave I made the same example but I used renders as state instead of useRef and got the same result, so what would be the difference?
@DaveGrayTeachesCode Жыл бұрын
You just stated the difference.
@andrelis7772 жыл бұрын
Hi, Dave. What are the downsides if we declare the timerId variable outside the App function and then manipulate it instead of using ref?
@DaveGrayTeachesCode2 жыл бұрын
I would need to explore that route as I made this video to demonstrate the useRef hook a few months ago.
@dimastriraharjo12842 жыл бұрын
Hi Dave, I found a bug in the timer. When clicking the start more than one, as we expect the counting is faster as clicking two times means +2 each but that when clicking stop, the counting only stop the latest start, the rest counting still continue. What's happening there? How can we completely stop all the counting?
@DaveGrayTeachesCode2 жыл бұрын
It has been awhile, but if I remember correctly, that bug was intentional to demonstrate what you described as expected. Either way, simple example for demonstration purposes only. If you want to clear any previous timer before starting another one, add this line of code as the first line of your startTimer function: if (timerId.current) clearInterval(timerId.current);
@dimastriraharjo12842 жыл бұрын
@@DaveGrayTeachesCode ok now its not going to double the interval because of checking the timerId is the same but I still dont quite get it with this .current is it like mentioning the current id of something that can be use to do something?
@DaveGrayTeachesCode2 жыл бұрын
@@dimastriraharjo1284 the current property always hold the value of the ref you create: reactjs.org/docs/hooks-reference.html#useref
@AM-nm6ts2 жыл бұрын
hello :) thanks a lot!!! just to confirme, it's a bad practice if I use useRef instead of useState to grab the input value to avoid a rerender ??
@DaveGrayTeachesCode2 жыл бұрын
I would only do that if necessary. useRef exists to persist a value between renders when needed, but not to avoid renders.
@AM-nm6ts2 жыл бұрын
@@DaveGrayTeachesCode TANK YOU A LOT 🙏❤👍 thank you for your time and the answer
@asthedreamfadesaway2 жыл бұрын
Hey Dave I was wondering, why is it bad to manage input values with useRef? In case I don't need my app to re-render each time my input value changes, what should I use? I wonder if that is the approach the library react-hook-form used to optimize the input re-rendering
@DaveGrayTeachesCode2 жыл бұрын
Good question, Diego! 💯 That is not the *normal* React approach to state, but it can be accomplished. There is a long article on it here: www.smashingmagazine.com/2020/11/react-useref-hook/ ...That said, React-Hook-Form does not seem to be using this approach. I didn't dive into exactly what is going on in their custom hooks though. There is some reference of their optimization in the docs here: react-hook-form.com/advanced-usage/#ControlledmixedwithUncontrolledComponents
@Sky-yy3 жыл бұрын
Amazing.
@DaveGrayTeachesCode3 жыл бұрын
Thank you! 💯
@Netpilation Жыл бұрын
you must have forgotten ,but you could have incremented the renders value inside useEFFEt and there was no need to increment in every function
@quasi_verum Жыл бұрын
the timer has bug, while you click start multiple time, the timer become faster. and can't be stopped.
@DaveGrayTeachesCode Жыл бұрын
I haven't checked this, but if it is an issue, you could disable the start button after clicked until it is stopped.