usePrevious β
A lightweight React hook that returns the previous value of a state or prop from the last render.
Perfect for comparing current and past values to detect changes over time.
π Overview β
usePrevious captures and returns the value from the previous render cycle.
Itβs often used in debugging, change detection, and implementing custom animations or transitions when a value changes.
π Features β
- π Tracks the previous render value
- π§ Works with any data type (
number,string,object, etc.) - β‘ Lightweight and dependency-free
- π§© Perfect for comparison logic and debugging
π‘ Example β
tsx
import { useState, useEffect } from "react";
import { usePrevious } from "react-hookstack";
export default function Counter() {
const [count, setCount] = useState(0);
const prevCount = usePrevious(count);
useEffect(() => {
if (prevCount !== undefined && prevCount !== count) {
console.log(`Count changed from ${prevCount} β ${count}`);
}
}, [count, prevCount]);
return (
<div>
<h3>Current: {count}</h3>
<p>Previous: {prevCount ?? "β"}</p>
<button onClick={() => setCount((c) => c + 1)}>Increment</button>
</div>
);
}π§© API Reference β
Signature β
tsx
const prevValue = usePrevious<T>(value: T): T | undefined;Parameters β
| Name | Type | Description |
|---|---|---|
value | T | The current value to track. |
Returns β
| Type | Description |
|---|---|
T | undefined | The previous value from the last render. Returns undefined on the initial render. |
π§ When to Use β
- Tracking previous props or state values
- Comparing current and old values to trigger side effects
- Implementing custom animations or transitions
- Debugging state changes in components
π§© Related Hooks β
useToggle - Toggle boolean states easily.
useCounter - Manage numeric state with increment/decrement helpers.
useRenderCount - Track how many times a component has rendered.
βοΈ Implementation β
tsx
import { useEffect, useRef } from "react";
/**
* A hook that stores and returns the previous value from the last render.
*/
export function usePrevious<T>(value: T): T | undefined {
const ref = useRef<T | undefined>(undefined);
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}
export default usePrevious;