Web Development

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

A

Adil Sher

Author

Jul 29, 2026
8 min read
10 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

Why I'm Done Selling Clients Off-the-Shelf Themes
Web Development Nov 2

Why I'm Done Selling Clients Off-the-Shelf Themes

I had this conversation last month with a real estate agent in Rawalpindi. She wanted a website, found a "premium" template on ThemeForest, and asked if I could just install it. The template looked polished, smooth animations, modern design, the works. I said yes, and three weeks...

Why I'm Rethinking How We Handle AI in Production Apps
Web Development Nov 1

Why I'm Rethinking How We Handle AI in Production Apps

Last month, I was sitting in a standup meeting when someone asked how we'd handle our LLM API if the provider went down. The room went quiet. We'd built our entire feature around OpenAI's reliability, with barely a fallback in sight. That conversation stuck with me, mostly because...

Stop Pretending Your RAG System Knows What It Doesn't
Web Development Oct 31

Stop Pretending Your RAG System Knows What It Doesn't

Last month, a client asked me to build a "smart document search" for their internal knowledge base. I nodded, quoted them a timeline, and started architecting the usual suspects: vector embeddings, semantic search, a language model API call. Two weeks into development, during a d...