Author -  Sai gowtham

How to use React useRef hook with examples

In this tutorial, we are going to learn about how to access the dom nodes in react by using useRef hook.

React hooks are JavaScript functions which helps us to use the missing react features in functional components.

useRef hook

In react useRef hook helps us to access the dom nodes or html elements, so that we can interact with that dom element like accessing the input element value or focussing the input element.

example:

import React,{useRef} from 'react';

function TextInput(){
   //creating the ref by passing initial value null
  const nameRef = useRef(null);
  function handleFocus(){
      //current property is pointing to the input element
      //when a component is mounts to dom
      nameRef.current.focus()  }
  return(
      <div>
          <input ref={nameRef} placeholder="name" />          <button onClick={handleFocus}>Focus</button>
      </div>
    )

}

In the above example, we have imported useRef hook from the ‘react’ then we invoked the useRef hook by passing the initial value null.

In input element we defined ref attribute by passing the nameRef so that we can access that input element by using nameRef.current property.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY