Web Development

React usePrevious Hook: Track Previous State & Props (2026)

A

Adil Sher

Author

Jul 29, 2026
8 min read
5 views
React usePrevious Hook: Track Previous State & Props (2026)

Summary

React usePrevious Hook: Track Previous State & Props (2026)

The Classic Recipe, and Where It Frays

return ( <div> <button onClick={() => setCount(count + 1)}>+1</button> <button onClick={() => setDark(!dark)}>toggle theme</button> {prevCount !== undefined && prevCount !== count && ( <span>{count > prevCount ? "↑ went up" : "↓ went down"}</span> )} </div> ); }

usePrevious, Previous Value, Not Previous Render

function Counter() { const [count, setCount] = useState(0); const [dark, setDark] = useState(false); const prevCount = usePrevious(count);

return ( <div> <button onClick={() => setCount(count + 1)}>+1</button> <button onClick={() => setDark(!dark)}>toggle theme</button> <p>Now: {count}, before: {prevCount ?? "-"}</p> </div> ); }

if (value !== current) { setPrevious(current); setCurrent(value); }

return previous; }

Wait, setState During Render?

The Gotcha: Unstable Objects

usePrevious vs useLatest

Real Use Cases

SSR Safety

Takeaways

Source

This article discusses content originally published by at Dev.to.

Read the original article

Share this article

Written by Adil Sher

Full stack developer building high-traffic platforms, AI services, and custom web applications. Explore my portfolio, learn about my background, or get in touch.

Related Articles

Stop Selling Your Code. Start Selling Solutions.
Web Development Sep 10

Stop Selling Your Code. Start Selling Solutions.

I've been grinding as a freelancer in Islamabad for five years now, and I've made every mistake in the book. I've spent weeks perfecting a portfolio that nobody reads. I've engaged in architecture debates with CTOs that went nowhere. I've underbid jobs and overworked myself becau...