The main difference between both is :

useState causes re-render, useRef does not.

The common between them is, both useState and useRef can remember their data after re-renders. So if your variable is something that decides a view layer render, go with useState. Else use useRef

I would suggest reading this article.

Answer from Easwar on Stack Overflow
🌐
Leewarrick
leewarrick.com › blog › react-use-effect-explained
React's useEffect and useRef Explained for Mortals | Strings and Things
September 6, 2019 - Update 9/7/19: It turns out, there’s a solution for simple examples like above, as pointed out by John Tucker (thanks John!). Much like setState in class-based components, useState can also accept a callback function that receives the previous state as an argument. The React Docs also make note of this. ... This still doesn’t solve all of our problems, though. If you need to access to the latest state inside useEffect, but not update it, you would have to start wrapping your useEffect code in setState callbacks, and then returning the unchanged state at the end.
Top answer
1 of 11
295

The main difference between both is :

useState causes re-render, useRef does not.

The common between them is, both useState and useRef can remember their data after re-renders. So if your variable is something that decides a view layer render, go with useState. Else use useRef

I would suggest reading this article.

2 of 11
19

useRef is useful when you want to track value change, but don't want to trigger re-render or useEffect by it.

Most use case is when you have a function that depends on value, but the value needs to be updated by the function result itself.

For example, let's assume you want to paginate some API result:

const [filter, setFilter] = useState({});
const [rows, setRows] = useState([]);
const [currentPage, setCurrentPage] = useState(1);

const fetchData = useCallback(async () => {
  const nextPage = currentPage + 1;
  const response = await fetchApi({...filter, page: nextPage});
  setRows(response.data);
  if (response.data.length) {
    setCurrentPage(nextPage);
  }
}, [filter, currentPage]);

fetchData is using currentPage state, but it needs to update currentPage after successful response. This is inevitable process, but it is prone to cause infinite loop aka Maximum update depth exceeded error in React. For example, if you want to fetch rows when component is loaded, you want to do something like this:

useEffect(() => {
  fetchData();
}, [fetchData]);

This is buggy because we use state and update it in the same function.

We want to track currentPage but don't want to trigger useCallback or useEffect by its change.

We can solve this problem easily with useRef:

const currentPageRef = useRef(0);

const fetchData = useCallback(async () => {
  const nextPage = currentPageRef.current + 1;
  const response = await fetchApi({...filter, page: nextPage});
  setRows(response.data);
  if (response.data.length) {
     currentPageRef.current = nextPage;
  }
}, [filter]);

We can remove currentPage dependency from useCallback deps array with the help of useRef, so our component is saved from infinite loop.

Discussions

useState vs useRef
I know the difference, useRef doesn't trigger a re-render. But I'm still having a hard time wrapping my head around why we use it, as I had to use it… More on reddit.com
🌐 r/reactjs
29
5
February 3, 2025
Can someone please explain the difference between useState and useReducer hook like I'm 5?
Their goal is the same: keeping state. With useState, you keep track of a single variable at a time: UseReducer allows for more complex state keeping, but you’ll have to write a function to merge new state with previously existing state yourself. The advantage is often you can dispatch “actions” on your state, the standard example being “increment” and “decrement”. So instead of updating state to state + 1, you can simply dispatch “increment”. Basically: as you write the implementation yourself, you have more freedom in how state is structured and how it’s updated based on existing state. More on reddit.com
🌐 r/reactjs
68
166
January 7, 2022
🌐
Reddit
reddit.com › r/reactjs › usestate vs useref
r/reactjs on Reddit: useState vs useRef
February 3, 2025 - ... Other cases, can be for example ... user move the mouse. With a useRef you save the coordinates and then you can choose when trigger the re-render if you want trigger it and you can change the logic....
🌐
LogRocket
blog.logrocket.com › home › usestate vs. useref: similarities, differences, and use cases
useState vs. useRef: Similarities, differences, and use cases - LogRocket Blog
June 4, 2024 - Access previous state with the help of useRef. It’s important to note that all refs need to get updated either inside a useEffect callback or inside handlers. Mutating the ref during rendering, i.e., from places other than those just mentioned, might introduce bugs. The same applies to useState, ...
🌐
DEV Community
dev.to › trinityyi › when-to-use-useref-instead-of-usestate-3h4o
When to use useRef() instead of useState() - DEV Community
November 26, 2022 - Okay, now I understand the point! When I used a counter to observe the component's re-rendering, I realized that using useRef retains the state throughout the entire lifecycle of the component. So, every time it re-renders, it doesn't start with the count of 0 again unlike useState.
🌐
Medium
medium.com › @ksshravan667 › 14-days-of-react-day-5-react-hooks-usestate-useref-useeffect-usememo-usecallback-8599a14c4e2b
14 Days of React — Day 5 — React Hooks: UseState, UseRef, UseEffect, UseMemo, UseCallback | by ksshravan | Medium
January 14, 2024 - Only useState and useReducer trigger ... and stores the actual function itself in a variable. useEffect actually executes the code and unlike the other 2 returns nothing....
🌐
React
legacy.reactjs.org › docs › hooks-reference.html
Hooks API Reference – React
During subsequent re-renders, the first value returned by useState will always be the most recent state after applying updates. ... React guarantees that setState function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the useEffect or useCallback ...
Find elsewhere
🌐
Alma Better
almabetter.com › bytes › tutorials › reactjs › reactjs-hooks
React JS Hooks: useState, useEffect, useRef and Custom Hooks
October 18, 2023 - There are two types of hooks in ... code reuse, improved performance, and easier testing. The useState hook allows functional components to have stateful logic, while the useEffect hook allows performing side effects....
🌐
DEV Community
dev.to › emmanuel_xs › the-difference-between-the-usestate-and-useref-hooks-3g83
The Difference between the useState() and useRef() Hooks - DEV Community
June 29, 2024 - The useEffect hook triggers the animation when the component mounts, moving the element 100 pixels to the right over 2 seconds. ... Changes to values managed by useState will trigger a re-render of the component.
🌐
Blog
blog.thesshguy.com › usestate-vs-useref
useState vs useRef: Choosing the Right Hook - Blog - Sai Hari
July 25, 2024 - But as a general rule of thumb use the useState hook when you want your component to re-render and the useRef hook when you don't want your component to re-render. ... React's useEffect hook is an important tool for managing side effects in ...
🌐
GeeksforGeeks
geeksforgeeks.org › difference-between-usestate-and-useeffect-hook-in-reactjs
Difference Between useState and useEffect Hook in ReactJS | GeeksforGeeks
October 14, 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 ElementsPrerequisites:React JSReact JS ...
🌐
Francisco Moretti
franciscomoretti.com › blog › usestate-vs-useref
useState vs useRef - When to use state and when to use ref
May 21, 2024 - Learn the differences between useState and useRef in React for state management and persisting values without re-renders.
🌐
SheCodes
shecodes.io › athena › 125174-difference-between-usestate-and-useeffect-in-react
[React] - Difference between useState and useEffect in | SheCodes
Learn about the difference between useState and useEffect hooks in React, and how they are used for state management and side effects.
🌐
Medium
medium.com › @kadampritesh46 › exploring-react-hooks-understanding-usestate-useeffect-usecontext-and-useref-00e767aa9729
Exploring React Hooks: Understanding useState, useEffect, useContext, and useRef | by Pritesh kadam | Medium
February 29, 2024 - The useRef Hook is used to create mutable references to elements or values. It persists across renders and does not trigger re-renders when its value changes.
🌐
Medium
medium.com › @rdhamnaskar11 › understanding-of-when-to-use-usestate-and-useref-hooks-6f7f60f79c82
Understanding of when to use useState() and useRef() hooks | by Ritesh M. Dhamnaskar | Medium
January 3, 2024 - In this article, we have learned that useRef and useState are both essential hooks in React, but they serve different purposes. useRef is a hook that allows you to access and manipulate the DOM directly.
🌐
Medium
medium.com › @ignatovich.dm › how-react-hooks-work-under-the-hood-recreating-usestate-useeffect-and-useref-from-scratch-65edaaa1a39b
How React Hooks Work Under the Hood: Recreating useState, useEffect, and useRef from Scratch | by Frontend Highlights | Medium
November 14, 2024 - useEffect: checks dependencies at index 1, and if they have changed, it runs cleanupFn and applies a new effect. useRef: returns the object at index 2, preserving the same current value across renders.
🌐
NashTech Blog
blog.nashtechglobal.com › home › when to use useref instead of usestate in reactjs
When to use useRef instead of useState in reactjs - NashTech Blog
November 8, 2024 - Understanding the useState Hook It is a built-in React hook that allows you to add a state to functional components. It takes an initial value as an argument and returns an array with two elements: the current state and a function to update that state. And it triggers a re-render component when its value changes.
🌐
Naomiajacobs
naomiajacobs.com › useRef-vs-useState
useRef vs useState – It's Not Magic - By Naomi Jacobs
October 20, 2020 - TL;DR Use useRef when you need information that is available regardless of component lifecycle and whose changes should NOT trigger rerenders. Use useState for information whose changes should trigger rerenders.