---
title: "ReactJS | useRef Hook with Example"
url: "https://magecomp.com/blog/reactjs-useref-hook/"
date: "2022-10-26T10:00:28+00:00"
modified: "2025-02-27T10:07:29+00:00"
author:
  name: "Bharat Desai"
  url: "https://magecomp.com"
categories:
  - "ReactJS"
word_count: 209
reading_time: "2 min read"
summary: "Hello ReactJS friends,"
description: "useRef hook in ReactJS is used to directly access the DOM element. Learn more about useRef hook with example"
keywords: "ReactJS"
language: "en"
schema_type: "Article"
related_posts:
  - title: "ReactJS | Handling Events with Example"
    url: "https://magecomp.com/blog/reactjs-handling-events-with-example/"
  - title: "Hook Error Handling doesn’t Catch Errors in ReactJS"
    url: "https://magecomp.com/blog/hook-error-handling-doesnt-catch-errors-reactjs/"
  - title: "ReactJS | Lists and Keys Tutorial with Example"
    url: "https://magecomp.com/blog/reactjs-lists-and-keys/"
---

# ReactJS | useRef Hook with Example

_Published: October 26, 2022_  
_Author: Bharat Desai_  

![ReactJS useRef Hook with Example](https://magecomp.com/blog/wp-content/uploads/2022/10/ReactJS-useRef-Hook-with-Example-1024x512.webp)

**Hello ReactJS friends,**

I have already explained about [**useEffect**](https://magecomp.com/blog/reactjs-useeffect-hook/), [**useState**](https://magecomp.com/blog/reactjs-usestate-hook/), and [**useContext**](https://magecomp.com/blog/reactjs-usecontext-hook/) Hook in ReactJS. Today we will learn about useRef Hook in detail.
[![React js](https://magecomp.com/blog/wp-content/uploads/2025/02/React-js-CTA-4.webp)](https://magecomp.com/hire-reactjs-developer.html)

## React UseRef Hook
useRef hook can be used to directly access the DOM element. It allows you to persist values between renders. It can be used to store a mutable value across renders and does not cause a re-render when updated.
When trying to count how many times our application renders using the useState Hook, we would be caught in an infinite loop since this Hook itself causes a re-render. Here comes the use of the useRef hook in ReactJS.
## Syntax of useRef Hook in ReactJS:
```
const refContainer = useRef(initialValue);
```

## [![React js](https://magecomp.com/blog/wp-content/uploads/2025/02/React-js-CTA-1.webp)](https://magecomp.com/services/contact-us/)

## Example of useRef Hook in ReactJS:
```
import React, { Component, createRef } from "react";

Const CustomTextInput = () => {
      textInput = createRef();
      focusTextInput = () => this.textInput.current.focus();
      return (
             <>
                 <input type="text" ref={this.textInput} />
                 <button onClick={this.focusTextInput}>Focus
                 the text input</button>
             </>
      );
}
```

## Conclusion:
This way, you can use useRef Hook in ReactJS. If you have any queries, share them with me through the comments and I will promptly reply to you. Share the tutorial with your friends and stay in touch with us to learn more about ReactJS.
[![React js](https://magecomp.com/blog/wp-content/uploads/2025/02/React-js-CTA-2.webp)](https://magecomp.com/hire-reactjs-developer.html)


---

_View the original post at: [https://magecomp.com/blog/reactjs-useref-hook/](https://magecomp.com/blog/reactjs-useref-hook/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-07-30 03:06:06 UTC_  
