Interact and Update State in React with useState

Share this video with your friends

Send Tweet

In this lesson, we'll create a Stopwatch component that will store the current time in its state. We will then have the DOM display the current time as the state is updated.

We'll cover:

  • React's useState hook
  • Setting a reference to an interval using React's useRef
  • Creating a handleRunClick function that handles flipping the running state plus creating and clearing an interval
  • Using the useEffect hook to clear the interval when the component unmounts
Vadym Kalinin
Vadym Kalinin
~ 6 years ago

Thanks for the great overview of new stuff! If I understood correctly, using normal variable for intervalRef instead of react reference, wouldn't change anything. But then, why did you go with react ref for it?

Kent C. Dodds
Kent C. Dodds(instructor)
~ 6 years ago

Not quite. The fact that it "works" when you try that is actually a side-effect of the fact that it's happening so quickly. Dig a little deeper and you'll see that it doesn't actually do what you think it's doing if you don't use the ref.

Krasimir
Krasimir
~ 6 years ago

I'm struggling to see the difference between useRef and useState in the context of this example. Are they interchangeable for keeping the interval ref?

Oroneki
Oroneki
~ 6 years ago

Great to know that we can use refs to something other than DOM nodes. So many info in just 5 minutes!

Kent C. Dodds
Kent C. Dodds(instructor)
~ 6 years ago

Krasimir, we could actually use useState for the intervalId, and then call the updater (setIntervalId) instead of setting intervalId.current. The difference is that with useState, using the updater will trigger a re-render. In our case we don't need a re-render when the interval id is set so it's better to use useRef.

Krasimir
Krasimir
~ 6 years ago

Ah right. It makes sense. Thanks for the clarification.

Kalin Chernev
Kalin Chernev
~ 6 years ago

It's no doubt a useful tip to use useRef instead of a variable - thanks! Is it something related to how the browser works with clearInterval or rather React's hook internals specifics that it doesn't work as expected with a variable rather than a value of useRef?

Kent C. Dodds
Kent C. Dodds(instructor)
~ 6 years ago

It's more just about how JavaScript works. Every time the component is rendered, that function is called again. We need to have some mechanism to keep track of that value between renders and that's what refs are intended for.

Kalin Chernev
Kalin Chernev
~ 6 years ago

Thanks!

vassiliskrikonis
vassiliskrikonis
~ 6 years ago

How about a variable outside the component function?

Kent C. Dodds
Kent C. Dodds(instructor)
~ 6 years ago

That would work, but what if I want to render more than one in my app?

Gaurav K
Gaurav K
~ 6 years ago

Thanks for explaining useRef in this example.

Here is the official documentation around this usage. https://reactjs.org/docs/hooks-reference.html#useref https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables

Quang Le
Quang Le
~ 6 years ago

Thanks Gaurav for pointing out the detail documents about useRef. I can see some related phrases like

  • "useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.", and
  • "useRef will give you the same ref object on every render.", I still prefer the clearer and more practical usage in this "some mechanism to keep track of that value between renders and that's what refs are intended for" from Kent. It is an excellent video!
~ 3 years ago

Looks like the code is no longer at sandbox...