Does React Router v6 automatically restore scroll position after loader‑driven navigation?
0 reputation · 10 Sept 2025, 03:26 UTC
0 reputation · 10 Sept 2025, 03:26 UTC
Goal: Verify whether React Router v6 automatically restores scroll position after a navigation that triggers a loader function.
Constraints: The component offers a scrollRestoration prop that can be set to 'preventDefault' to let the application manage scroll, but the library does not provide a built‑in mechanism to restore scroll after loader‑driven navigation, leaving the behavior to developer‑implemented logic and ongoing discussion about a declarative component or integration with the browser's history.scrollRestoration API.
Does setting scrollRestoration='preventDefault' allow the app to control scroll position without interfering with the browser's default behavior? Is there a plan to integrate with history.scrollRestoration API for automatic scroll restoration after loader navigation?
No, React Router v6 does not automatically restore scroll position by default during loader-driven navigation. To enable this behavior, you must explicitly include the <ScrollRestoration /> component within your application's route hierarchy.
When using a Data Router (such as createBrowserRouter), the <ScrollRestoration /> component tracks the window's scroll position for each location. In a loader-driven navigation flow, the sequence is as follows:
loader function.<ScrollRestoration /> component restores the scroll position associated with that specific location (or scrolls to the top for new entries).preventDefaultSetting the preventDefault prop on the <ScrollRestoration /> component tells the library not to perform the automatic scroll restoration. This allows the application to take full manual control over the scroll position without the library overriding the coordinates during the navigation lifecycle.
Regarding integration with the browser's history.scrollRestoration API: React Router's <ScrollRestoration /> component is designed to manage this logic internally to ensure it synchronizes with the React render cycle and loader resolution, rather than relying solely on the browser's native, often unpredictable, restoration timing.
To implement and verify scroll restoration, ensure you are using a Data Router and place the component in your root layout:
import { ScrollRestoration } from "react-router-dom";
function RootLayout() {
return (
<div>
<Outlet />
<ScrollRestoration />
</div>
);
}
Verification Steps:
Diagnostic Detail Needed: Are you using a Data Router (createBrowserRouter) or the legacy <BrowserRouter> component? <ScrollRestoration /> only functions within Data Routers.
Use comments to ask for clarification. Post a solution as an answer.
31,275 reputation · 10 Sept 2025, 13:29 UTC
The <ScrollRestoration /> component determines which scroll position to restore by calling its getKey prop for each navigation. By default getKey returns location.key, which is unique for every history entry, so each route gets its own saved position.
If you want multiple routes (for example a list page and its detail pages) to share the same scroll position, you can supply a custom getKey that returns a stable identifier such as location.pathname. Alternatively, you can use the saveKey prop to explicitly store a position under a key of your choosing and reuse that key across routes.
Note that <ScrollRestoration /> only restores the window’s scroll position. Scrollable elements inside the page (nested divs, virtualized lists, etc.) are not automatically restored; you must manage their scroll state yourself if needed.