How to Remember the Previous Render’s Value with useRef in React
If you store a value in useRef and overwrite it inside an effect, the next render can read the value from the render before it. This pattern is useful when you want to show whether a number such as a counter or price went up or down since the last update, or when you want to change behavior based on the previous value. However, this approach has a quirk: when an unrelated re-render happens, the previous value becomes the same as the current value. For showing the "value before the change" on screen, the state-based approach described in the official documentation was more reliable in my testing.
The test environment was Ubuntu 24.04 with Node.js 22.23.4. I used react and react-dom 19.3.1, @types/react 19.3.0, TypeScript 6.0.3, esbuild 0.28.2, ESLint 9.39.5, and eslint-plugin-react-hooks 7.1.1. I type-checked the .tsx files with tsc, built a development bundle with esbuild, and ran Chromium 153 with Playwright 1.63.0 to click the buttons. I did not test production builds, code run through the React Compiler, Firefox, or Safari.
- 1. Returning the previous render's value with useRef and an effect
- 2. An unrelated re-render makes the previous value equal the current value
- 3. Store the previous value in state if you want to show it
- 4. usePrevious triggers an error from the ESLint refs rule
- 5. If you only compare with the previous value, read the ref inside an effect
Returning the previous render's value with useRef and an effect
A custom hook that returns the previous render's value is sometimes named usePrevious. It consists only of a box created with useRef and an effect that runs after the render finishes.
import { useEffect, useRef } from 'react';
// Returns the value from the previous render
export function usePrevious<T>(value: T): T | undefined {
const ref = useRef<T | undefined>(undefined);
useEffect(() => {
ref.current = value;
});
return ref.current;
}
The mechanism depends on the order of reading and writing. Reading ref.current with return ref.current during render gets the value that the effect stored after the previous render. As described in the useEffect reference, an effect runs after the result of this render has been reflected in the DOM (committed). The current value is written into ref.current at that point, so it can only be read from the next render onward. As described in the useRef reference, changing ref.current does not trigger a re-render, so the new value only appears on screen the next time a re-render happens.
Using this hook, I built a counter that shows the current value next to the previous render's value. I also added a button that updates a state unrelated to count, only to trigger a render. log is a helper I wrote for the test that records one line per render.
import { useState } from 'react';
import { usePrevious } from './usePrevious';
import { log } from './log';
export function RefCounter() {
const [count, setCount] = useState(0);
const [tick, setTick] = useState(0);
const prevCount = usePrevious(count);
log(`描画 RefCounter count=${count} prevCount=${prevCount}`);
return (
<div>
<p>今の値:{count}</p>
<p>前のレンダーの値:{prevCount ?? 'なし'}</p>
<button onClick={() => setCount(count + 1)}>+1</button>
<button onClick={() => setCount(count - 1)}>-1</button>
<button onClick={() => setTick(tick + 1)}>別の state を更新({tick})</button>
</div>
);
}
The className attributes I used during testing are omitted from the code in this article.
An unrelated re-render makes the previous value equal the current value
In the test environment, I clicked "+1", "+1", "Update another state", "-1", and "Update another state" in that order. The log looked like this:
--- initial display
描画 RefCounter count=0 prevCount=undefined
--- +1
描画 RefCounter count=1 prevCount=0
--- +1
描画 RefCounter count=2 prevCount=1
--- Update another state
描画 RefCounter count=2 prevCount=2
--- -1
描画 RefCounter count=1 prevCount=2
--- Update another state
描画 RefCounter count=1 prevCount=1
On the initial display, the effect has not run yet, so the value is undefined. After two clicks of "+1", the previous render's values were 0 and 1. However, after "Update another state", the previous value also became 2, even though count was still 2. Since count was 2 in the immediately preceding render as well, this is a correct result for "the previous render's value."
The problem arises when you want the "value before it changed." In the result above, just updating an unrelated state erased the information that the value had gone from 1 to 2.
Store the previous value in state if you want to show it
The useRef reference says not to read or write ref.current during rendering, except during initialization. The same page, in its section on referencing a value with a ref, recommends using useState when you must read or write during rendering. The example it points to is the section of the useState reference titled "Storing information from previous renders."
Following the pattern in that section, I stored the previous render's count in a state called lastCount and the value before the change in a state called prevCount. Both are updated during rendering, but only when count changes.
import { useState } from 'react';
import { log } from './log';
export function StateCounter() {
const [count, setCount] = useState(0);
const [tick, setTick] = useState(0);
const [prevCount, setPrevCount] = useState<number | undefined>(undefined);
const [lastCount, setLastCount] = useState(count);
if (lastCount !== count) {
setPrevCount(lastCount);
setLastCount(count);
}
log(`描画 StateCounter count=${count} prevCount=${prevCount}`);
return (
<div>
<p>今の値:{count}</p>
<p>変わる前の値:{prevCount ?? 'なし'}</p>
{/* Buttons are the same as in RefCounter */}
</div>
);
}
This is the log when I clicked the buttons in the same order.
--- initial display
描画 StateCounter count=0 prevCount=undefined
--- +1
描画 StateCounter count=1 prevCount=undefined
描画 StateCounter count=1 prevCount=0
--- +1
描画 StateCounter count=2 prevCount=0
描画 StateCounter count=2 prevCount=1
--- Update another state
描画 StateCounter count=2 prevCount=1
--- -1
描画 StateCounter count=1 prevCount=1
描画 StateCounter count=1 prevCount=2
--- Update another state
描画 StateCounter count=1 prevCount=2
After "Update another state", the value before the change stayed at 1 or 2. When count changed, two log lines were recorded. The first line is from the render that calls the set function during rendering, and that render is discarded. The same section of the useState reference also says that calling a set function during rendering causes React to re-render immediately after the return, running the rest of the function but discarding its result. The value that appeared on screen was from the second line.
The following image shows two counters stacked, with the same sequence of button clicks.

When calling a set function during rendering, wrap it in a condition such as lastCount !== count. According to the same section, calling it unconditionally causes repeated re-renders and crashes the component.
usePrevious triggers an error from the ESLint refs rule
usePrevious reads ref.current during rendering. When I checked the test code with the recommended config of eslint-plugin-react-hooks 7.1.1, only the return ref.current; line in usePrevious was reported as an error. StateCounter and the effect-based code compared in the next section produced no errors.
/path/to/app/src/usePrevious.ts
9:10 error Error: Cannot access refs during render
This output is trimmed to its first two lines, and the path is replaced with a placeholder. The rule name is react-hooks/refs. The official documentation for the refs rule lists both reading and writing ref.current during rendering as examples the rule rejects.
If you only compare with the previous value, read the ref inside an effect
If you do not want to display the previous value and only need to branch on whether it went up or down when it changes, read and write the ref only inside an effect. The useRef reference's section on referencing a value with a ref also says refs should be read and written inside event handlers or effects.
import { useEffect, useRef, useState } from 'react';
import { log } from './log';
export function EffectCompare() {
const [count, setCount] = useState(0);
const [tick, setTick] = useState(0);
const prevRef = useRef(count);
useEffect(() => {
const prev = prevRef.current;
if (prev !== count) {
log(`変化 ${prev} → ${count}(${count > prev ? '増えた' : '減った'})`);
} else {
log(`変化なし ${prev} → ${count}`);
}
prevRef.current = count;
}, [count]);
// The display (current value only) and buttons are the same as in the counters above
}
When I clicked the buttons in the same order, the log was:
--- initial display
変化なし 0 → 0
--- +1
変化 0 → 1(増えた)
--- +1
変化 1 → 2(増えた)
--- Update another state
--- -1
変化 2 → 1(減った)
--- Update another state
The dependency array is [count], so the effect did not run on "Update another state" and nothing was logged. The previous and current values were read together only when count changed.
Because useRef(count) stores the initial value, the previous and current values are the same on the initial display. That is why the code compares with prev !== count. In the development build with StrictMode enabled, "no change 0 → 0" was logged twice on the initial display. As described in the useEffect reference, StrictMode runs an extra setup and cleanup cycle for effects, but only during development. With the comparison in place, the "went up" and "went down" branches never run on that second call.
I also tested the usePrevious approach with StrictMode. The log had two lines per render, but the value on screen matched the non-StrictMode result, and the previous value still became equal to the current value after "Update another state."





