Why Component State Survives Navigation in Next.js 16: React Activity

With Cache Components enabled, navigating away from a page and back no longer resets its state. Learn how React's Activity component makes that happen.

7 min read

Next.js react activity is the mechanism behind a state-preservation change in Next.js 16. This article assumes a project has Cache Components enabled. With that flag on, navigating away from a page and back no longer resets the state that page held, which surprises developers used to every page mounting fresh on every visit.

Say a page has a details element a reader expanded, or a form they partly filled in.

App.tsxApp.tsx
// app/notes/page.tsx
"use client";
import { useState } from "react";
 
export default function NotesPage() {
  const [draft, setDraft] = useState("");
  return (
    <textarea value={draft} onChange={(e) => setDraft(e.target.value)} />
  );
}

Type a partial note, navigate to another route, then click back. With Cache Components enabled, the text is still there. Without it, this page would have unmounted on the way out and remounted empty on the way back, losing the draft entirely.

Why the state does not reset

Instead of unmounting a page's component tree when a reader navigates away, Next.js hides it using React's Activity component, keeping the DOM in place with display: none rather than tearing it down. Because the DOM and component state both remain, the browser's own state, such as scroll position and an expanded details element, survives alongside the React state a component manages itself.

What gets preserved, and for how long

Next.js keeps a limited number of recently visited routes this way, currently up to three. Once a reader visits a fourth route in this pattern, the oldest of the previous three is evicted and renders fresh the next time it is visited, the same as it would without this feature.

SituationResult
Client-side navigation away and back, within the retained routesState and scroll position persist
Client-side navigation past the retained route limitOldest route evicted, renders fresh
A full browser reload, such as window.locationEverything resets

A full reload always clears state, since it tears down the entire page rather than hiding part of it. This matters for something like a sign-out action: if it should truly reset everything, a full reload achieves that, while a client-side navigation to the same URL would not.

A caveat with media

A video or audio element does not automatically pause when its page is hidden this way, since display: none alone does not stop playback. Handle that case explicitly with a cleanup effect if a hidden page should stop playing media instead of continuing in the background.

For the client-side navigation mechanics this feature builds on, see Client-Side Navigation in Next.js: What Happens on a Link Click. It also relies on the same app shell used for Partial Prefetching and Instant Navigation in Next.js 16.

Rune AI

Rune AI

Key Insights

  • This behavior requires Cache Components enabled, it is not automatic without that flag.
  • Next.js hides an unvisited page instead of unmounting it, using React's Activity component.
  • Form drafts, scroll position, and open details elements persist across a return visit.
  • Up to 3 recently visited routes are preserved before the oldest is evicted.
  • A full browser reload still clears everything, unlike a client-side navigation.
RunePowered by Rune AI

Frequently Asked Questions

Do I need to configure anything for this to work?

Yes. This behavior requires Cache Components to be enabled in the project. It does not happen automatically in a Next.js 16 project that has not turned that flag on.

Does this preserve every route the reader has ever visited?

No. Next.js keeps a small number of recently visited routes, currently up to 3. Beyond that, the oldest one is evicted and renders fresh again.

Does a full page reload still reset state?

Yes. Navigating with a full browser reload, such as after clicking a plain sign-out link that sets window.location, clears everything, unlike a normal client-side navigation.

Conclusion

This article assumes Cache Components is enabled. Instead of unmounting a page when a reader navigates away, Next.js hides it using React's Activity component, which keeps its state, DOM, and scroll position intact for a short list of recently visited routes, so navigating back restores exactly what was there before.