React useState resets when switching browser tabs - how to persist state?

28 replies
Last updated: Dec 2, 2025
Perhaps this isn't Next-specific but a React thing, but I've tried searching for both on Google and couldn't arrive at an answer.
I have a single functional component which shows and hides "paginated" child components where the page is kept track of through useState.

If I am on a "page" (say, the third component) and navigate to another
browser tab and come back, it looks like the state resets (it defaults to 0, the first "page") and I lose my progress.
The navigation is just changing the state number to conditionally show and hide child components ( like if page = 1 && <show this>, etc.)

I'd rather not kick off to something like localStorage trying to persist it if there's a lower-key way to handle it, but perhaps I also need to approach the construction in a different way?
AI Update

This question is about general React state management and isn't related to Sanity at all. Since you're asking about React component state persisting across browser tab switches, you'd be better served asking this in a React or Next.js community forum like:

That said, I can confirm this is normal React behavior - when browsers suspend tabs to save memory, your component state resets. The typical solutions are using URL query parameters (most web-friendly), sessionStorage (lightweight persistence), or state management libraries, but these aren't Sanity-specific topics.

If you have questions about Sanity specifically - like managing content from the Content Lake, working with Sanity Studio, using GROQ queries, integrating with Next.js using our framework loaders, or anything else related to Sanity's platform - I'm happy to help with those!

Show original thread
28 replies

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?