Implement usePrevious Hook in React [Interview]
Understand the implementation and usage of usePrevious Hook in React to help tracking previous value in react app.
Anuj Sharma
Last Updated Dec 23, 2025
![Implement usePrevious Hook in React [Interview]](/_next/image?url=https%3A%2F%2Ftwijvresuxjxyhisasvd.supabase.co%2Fstorage%2Fv1%2Fobject%2Fpublic%2Fblog-images%2F5bf882d3-0967-44f5-bb29-5f33706cdfa6%2Fimplement-useprevious-hook-in-react-interview-1766513890657.png&w=3840&q=75&dpl=dpl_2enarHx6DLyMFgKTMt5UsAntqGh1)
While developing react applications, sometimes you need to keep track of the previous value of a state or a prop to perform certain actions or comparisons.
The usePrevious custom hook in React allows you to store the previous value of a state or a prop to be accessed in subsequent renders.
React does not provide a built-in way to track the previous value, so this hook is extremely useful in real-world apps and comes up often in interviews.
Implementation of usePrevious Hook
Let's create the usePrevious custom hook in React
import { useRef, useEffect } from 'react';
const usePrevious = (value) => {
const ref = useRef();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
};
export default usePrevious;
Usage of usePrevious Hook
Now, let's see how to use the usePrevious hook in a React functional component
import React, { useState } from 'react';
import usePrevious from './usePrevious';
const ExampleComponent = () => {
const [count, setCount] = useState(0);
const prevCount = usePrevious(count);
return (
<div>
<p>Current count: {count}</p>
<p>Previous count: {prevCount !== undefined ? prevCount : 'N/A'}</p>
<button onClick={() => setCount(count + 1)}>Increment Count</button>
</div>
);
};
export default ExampleComponent;
Explanation
In the above example, the usePrevious hook is used to keep track of the previous value of the count state. The hook returns the previous value of the count state, which is then displayed in the component.
Use-cases for usePrevious hook
usePrevious is useful when you need to:
- Compare previous and current values: Useful for triggering some action only when a value changes.
- Animate on value change: Animate a number counter when the value updates.
- Track prop or state differences: Sometimes you want to know how a prop changed across renders.
- Debug unexpected re-renders: Because the previous value is
undefinedon the first render, it works like a built-in check.
Learn Next 🚀
A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me 🚀
Comments
Be the first to share your thoughts!
No comments yet.
Start the conversation!
Share your expertise
Publish a blog or quick notes on topics you know well — your write-up could be the answer someone needs before their next frontend interview.
Build your portfolio
Help the community
Sharpen your skills
Earn goodies
Other Related Blogs
React Hook Rules: Why hooks declarations are not allowed inside functions
Frontendgeek
Last Updated Feb 6, 2026
A quick guide to explain an important react interview question, why React Hooks declarations are not allowed inside functions or any conditional blocks with code example.
setTimeout Polyfill in JavaScript - Detailed Explanation
Anuj Sharma
Last Updated Aug 3, 2025
Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.
Implementing a stopwatch using React - Frontend Machine Coding Question
Pallavi Gupta
Last Updated Feb 21, 2026
Concise explanation of stopwatch implementation using React, it involves the usage of useEffect hook for creating a stopwatch and tracking milliseconds.
Implement useClickOutside() custom Hook in React [Interview]
Anuj Sharma
Last Updated Dec 23, 2025
Understand the implementation of useClickOutside() custom hook in react and how it can be used to implement Modal like functionality.
