Difference between useRef and createRef in ReactJS
Last Updated :
24 Jul, 2024
The useRef is a hook used in functional components introduced in the React version 16.8 while createRef is a react method used in React class component, both are used to create a reference to the React Elements
Prerequisites:
A ref is defined as any value that does not trigger a component re-render when it is changed. This behavior is contrary to the function of states and props. A ref can be created in two ways- by the useRef hook or by the createRef function.
The useRef is a hook that uses the same ref throughout. It saves its value between re-renders in a functional component and doesn't create a new instance of the ref for every re-render. It persists the existing ref between re-renders.
The createRef is a function that creates a new ref every time. Unlike the useRef, it does not save its value between re-renders, instead creates a new instance of the ref for every re-render. Thus implying that it does not persist the existing ref between re-renders.
Let's look at an example to understand the differences more clearly.
Steps to Create React Application:
Step 1: Create a React application using the following command:
npx create-react-app react-ref
Step 2: After creating your project folder i.e. react-ref, move to it using the following command:
cd react-ref
Project Structure:

Example 1: This example use useRef hook to create reference to a component
JavaScript
// Filename - App.js
import React, { useEffect, useRef, useState } from "react";
export default function App() {
const [counter, setCounter] = useState(0);
const ref = useRef();
useEffect(() => {
ref.current = "GeeksforGeeeks";
}, []);
useEffect(() => {
console.log(counter, ref.current);
}, [counter]);
return (
<div
style={{
backgroundColor: "green",
height: "100vh",
padding: "50px",
}}
>
<h3>Example on useRef</h3>
<button
onClick={() => setCounter((c) => c + 1)}
>
Increment
</button>
<h5>Counter Value: {counter}</h5>{" "}
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Explanation: Since the useRef persists the ref value on re-renders, we can see the ref.current value on every re-render.
Example 2: This example uses createRef method to create reference to react element
JavaScript
// Filename - App.js
import React, {
useEffect,
createRef,
useState,
} from "react";
import "./App.css";
export default function App() {
const [counter, setCounter] = useState(0);
const ref = createRef();
useEffect(() => {
ref.current = "GeeksforGeeeks";
}, []);
useEffect(() => {
console.log(counter, ref.current);
}, [counter]);
return (
<div
style={{
backgroundColor: "green",
height: "100vh",
padding: "50px",
}}
>
<h3>Example on createRef</h3>
<button
onClick={() => setCounter((c) => c + 1)}
>
Increment
</button>
<h5>Counter Value: {counter}</h5>{" "}
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Explanation: Since the createRef doesn't persist the ref value on re-renders, we can see the ref.current value only once.
Difference between useRef and CreateRef :
useRef | createRef |
---|
It is a hook. | It is a function. |
It uses the same ref throughout. | It creates a new ref every time. |
It saves its value between re-renders in a functional component. | It creates a new ref for every re-render. |
It persists the existing ref between re-renders. | It does not persist the existing ref between re-renders. |
It returns a mutable ref object. | It returns a read-only ref object. |
The refs created using the useRef can persist for the entire component lifetime. | The refs created using the createRef can be referenced throughout the component. |
It is used in functional components. | It is used in class components. It can also be used in functional components but might show inconsistencies. |
Similar Reads
Difference between ReactJS and Vue.js
ReactJS: ReactJS is an open-source JavaScript library created by Facebook which is used to deal with the view layer for both Web and Mobile applications. It can be provided on the server-side along with working on the client-side. Features of ReactJS: Scalability: It is reasonable for enormous scale
2 min read
What is the difference between â(â¦);â and â{â¦}â in ReactJS ?
When you write JavaScript, you can use either the "(â¦)" or "{â¦}" pattern to define objects. In ReactJS, (...); and {...} are used in different contexts and have different purposes, and are used to denote different types of code structures. What is "(â¦);" in React JS ?In ReactJS, (...); is used to de
5 min read
Difference Between useState and useEffect Hook in ReactJS
ReactJS is the powerful JavaScript library used for building user interfaces especially single-page applications(SPAs). Two of the most commonly used hooks in React are useState and useEffect. These hooks make functional components more powerful by allowing them to manage the state and handle side e
3 min read
Difference between React.memo() and useMemo() in React.
In React applications, optimizing performance is crucial for delivering a smooth user experience. Two key tools for achieving this are React.memo() and useMemo(). While both help improve performance, they serve different purposes and are used in distinct scenarios. Table of Content What is React.mem
3 min read
Difference between React.js and Bootstrap
React JS is a JavaScript library for creating user interfaces while Bootstrap is a framework having pre-designed and styled components to create responsive UI. React dynamically builds the structure and Bootstrap add the format and styling to the components. What is React.js?ReactJS is a JavaScript
2 min read
Difference between React.Component and React.PureComponent?
A Component is one of the core building blocks of React. In other words, we can say that every application you will develop in React will be made up of pieces called components. But React has two types of Components:React.PureComponent: It is one of the most significant ways to optimize React applic
3 min read
What's the difference between super() and super(props) in React ?
Before going deep into the main difference, let us understand what is Super() and Props as shown below: Super(): It is used to call the constructor of its parent class. This is required when we need to access some variables of its parent class.Props: It is a special keyword that is used in react sta
3 min read
Difference Between a .js and .jsx File in React
React is a JavaScript library used for building user interfaces, especially for single-page applications. It uses a component-based structure that makes development more efficient and maintainable. In React, different file extensions are used for specific purposes: .js or .jsx for JavaScript and JSX
4 min read
What are the differences between Redux and Flux in ReactJS ?
During the phase of applications or software development, we gather the requirements of customers to create a solution to solve the problem of customers or businesses. To solve problems we rely on different technologies and architecture patterns. for a long time, developers were using MVC (Model-Vie
10 min read
Difference Between JavaScript and React.js
JavaScript is a versatile programming language widely used for creating interactive web pages and web applications. It is essential for front-end development, allowing developers to manipulate webpage elements, handle user interactions, and dynamically update content. On the other hand, React.js is
4 min read