Course lesson

Interact and Update State in React with useState

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.

Duration
5 min
Access
Free
Transcript
Available

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