Web Development

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

A

Admin User

Author

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

I Pushed Code for Years Without Understanding What Happened Next
Web Development Aug 3

I Pushed Code for Years Without Understanding What Happened Next

I remember the exact moment I realized I had no idea how my CI pipeline actually worked. I was debugging a flaky test in our staging environment, and a senior developer asked me: "Where is this test running?" I said "GitHub Actions." He asked: "On what machine?" Silence. I honest...