usePrevious Hook — Track Previous Value

Custom Hooks — Beginner React challenge.

Return the value a prop or state had on the previous render. A staple utility hook — trivial to write, tricky to get exactly right.

Requirements

Signature

function usePrevious<T>(value: T): T | undefined
function getDeltaClassName(current: number, previous: number | undefined): string

Example

const prev = usePrevious(count);
// count: 0 → prev: undefined
// count: 1 → prev: 0
getDeltaClassName(5, 3); // "delta-up"

Constraints

🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code
Loading workspace…