useRef Hook | useRef vs useState

Hey everyone ,

In this article, let us understand the useRef hook in React.js

Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Syntax


Β  Β  Β  Β CODEΒ for the video

Β  Β  Β  Β  Β  Β  Β  Β  Β  Ref Example – 1

Β  Β  Β  Β  Β  Β  Β (An Interval Timer)

  1. As a very first step, import the useState, useRef and useEffect hooks from React.
  2. Setup an initial state for counter and set this to a value of 0 to begin with.
  3. Next let us setup an identifierΒ ref using the useRefΒ hook and set this to an initial value of null to begin with. We are using a ref here instead of a normal variable because we want the identifier ref to persist its value across renders. Also if it changes, it won’t cause a re-render of the component.
  4. Next setup a useEffectΒ hook and here let us setup an interval using the setInterval function and here we want to update the value of counter each time after 1 second.
  5. We can persist the interval value by setting it to the current property of Β the identifier ref as shown below. This we will also use in just a second to clear the interval that gets setup when the component unmounts.
  6. So return a function from the useEffectΒ and let’s name it as clearIdentifier.
  7. So define the clearIdentifierΒ function as an arrow function and here we can just call the clearIntervalΒ function passing to it our ref value i.e identifier.currentΒ Remember the useEffectΒ willΒ only run once after every initial renderΒ as we have passed an empty array of dependencies..
  8. In our JSX, let us render the counter value and a button which can be used to clear the interval and stop the counter.

Ref Example – 2 (Working with DOM using refs)

  1. As a very first step, let us import the useEffect and useRef hooks from React.
  2. Setup two refs, one for email and other for password i.e emailRef and passwordRef and set them to an initial value of empty quotes to begin with.
  3. In the JSX that we return, let us setup two input fields inside a form element, one for email and other one for password, and place the created refs on each one of them respectively i.e emailRef on the email field and passwordRef on the password field.
  4. Now on the submission of the form, let us invoke a handleSubmission method. Here we want to tap the values of email and password from the form fields using the refs. So to access the value of an input field using the refs, we can just say :Β Β So to access the value for email from the form, we can say emailRef.current.value and to access the value for the password from the form, we can say passwordRef.current.value. So let us just log them to the console.

    Next let us setup a useEffectΒ hook and on the initial render, let us justΒ set focus on the email using the emailRef. So for this, add the following code :

Β  Β  Β  useState vs useRef

If you prefer video over article, then check this video where I cover about the useRef hook with the exact same examples.

πŸ“ΊΒ VIDEO

Thanks for reading !

Previous Post
Next Post