Track Previous State in React with One Powerful Hook

React functional components are clean and powerful but there’s one thing they don’t give you out of the box: access to the previous value of a prop or state.
That’s where usePrevious
comes in a lightweight, reusable hook that lets you track value changes like a pro.
View the full hook and live usage examples here
When do you need this?
Ever needed to compare the current value with the previous one to:
Trigger an animation?
Detect a value change?
Avoid unnecessary updates?
If yes, this tiny utility will save you time and logic.
The usePrevious
Hook
import { useRef, useEffect } from "react";
export const usePrevious = <T = any>(value: T): T | undefined => {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
};
Why use usePrevious
?
Compare current and previous values
Fully typed for TypeScript
Clean and reusable in any component
Explore usePrevious
with code examples and docs
Final Thoughts
Tracking previous state values in functional components can unlock new patterns and simplify your logic. Instead of juggling multiple variables or useEffect tricks, use a clean and tested hook.
Give it a try and integrate it into your next component usePrevious Hook with Live Examples.
Contribute or give a star for better reach: https://github.com/rijvi-mahmud/shaddy
Subscribe to my newsletter
Read articles from Rijvi Mahmud directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Rijvi Mahmud
Rijvi Mahmud
Hey, I'm Rijvi Mahmud, a frontend engineer with 2+ years of experience. I work with React.js, Next.js, TypeScript, ShadCN UI, Tanstack, Zustand, and Node.js, building clean and efficient user interfaces. I started my career remotely, and over time, well-known companies have trusted me for my reliability, ability to meet deadlines, clear communication, teamwork, and leadership skills. I believe in delivering high-quality software while staying available and adaptable to any situation. I'm someone who loves challenges, learns fast, and can work both independently and as part of a team. Whether it's planning things down to the last detail or working under tight deadlines, I stay focused and get the job done. I constantly improve by solving problems, reading documentation, and researching how other developers approach things. I also have a solid understanding of software best practices, SDLC, SOLID principles, and OOP, which help me write scalable and maintainable code.