MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Scroll Restoration and Optimistic Route Transitions0%
Study Materials
React.js for AdvancedProgress 0%
1.1Control Props and Inversion of Control Pattern1.2State Reducer Pattern in Advanced React1.3Prop Getters and Flexible Design Systems1.4Headless UI Architecture and Accessibility Primitives
4.1React Router v6 Data Loaders and Actions4.2Deferred Data and Suspense Streaming with Await4.3Protected Routes and Role-Based Access Guards4.4Scroll Restoration and Optimistic Route Transitions
HomeTutorialsReact.js for AdvancedScroll Restoration and Optimistic Route Transitions
Architecture of Server-Side Rendering vs Client-Side Rendering

Scroll Restoration and Optimistic Route Transitions

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. The Scroll Position Bug in SPAs

In traditional server-rendered websites, when a user scrolls halfway down a long catalog page, clicks a link to view a product, and then clicks the browser "Back" button:

  • The browser automatically restores the exact scroll position where they left off.

In a naive Single-Page Application:

  • Clicking a link navigates to a new view, but the viewport remains scrolled down at the previous position!
  • Clicking "Back" sends the user to the top of the catalog page, forcing them to search and scroll all over again!

This jarring UX flaw is solved in modern React Router via Scroll Restoration.

1Naive SPA:
2Scroll to item 50 ──► Click Detail ──► Viewport stays scrolled down! (BUG)
3 
4With <ScrollRestoration>:
5Scroll to item 50 ──► Click Detail ──► Smoothly resets to top (0,0)
6Click "Back" ──► Viewport restored PRECISELY to item 50! (Expected UX)

2. Implementing <ScrollRestoration />

In React Router v6 Data Routers (createBrowserRouter), you simply render <ScrollRestoration /> inside your root layout component:

1// src/layouts/RootLayout.jsx
2import React from 'react';
3import { Outlet, ScrollRestoration } from 'react-router-dom';
4import Navbar from '../components/Navbar';
5import Footer from '../components/Footer';
6 
7export default function RootLayout() {
8 return (
9 <div className="app-shell">
10 <Navbar />
11 
12 <main className="content">
13 <Outlet />
14 </main>
15 
16 <Footer />
17 
18 {/* Automatically restores scroll position on navigation! */}
19 <ScrollRestoration />
20 </div>
21 );
22}

Custom Scroll Keys (Tabs and Query Params)

By default, scroll positions are keyed by the URL pathname. If you have tabs that change query params (?tab=reviews) and want to preserve scroll independently:

1<ScrollRestoration
2 getKey={(location, matches) => {
3 // Return key string to match against
4 return location.pathname + location.search;
5 }}
6/>

3. Optimistic Route Transitions with useNavigation

When users click a link that loads data via a loader, there may be a 150–300ms delay while data fetches across the network.

Without visual feedback, the user wonders: "Did my click register?"

React Router provides the useNavigation() hook to inspect active in-flight transitions:

1import React from 'react';
2import { useNavigation } from 'react-router-dom';
3 
4export default function GlobalProgressBar() {
5 const navigation = useNavigation();
6 
7 // navigation.state: 'idle' | 'submitting' | 'loading'
8 const isNavigating = navigation.state !== 'idle';
9 
10 if (!isNavigating) return null;
11 
12 return (
13 <div className="top-progress-bar" role="progressbar">
14 <div className="progress-indeterminate-pulse" />
15 </div>
16 );
17}

4. Pending UI Styling with useNavigation

You can even dim the current screen while the next route is loading:

1export default function RootLayout() {
2 const navigation = useNavigation();
3 
4 return (
5 <div className={`app-shell ${navigation.state === 'loading' ? 'loading-opacity' : ''}`}>
6 <GlobalProgressBar />
7 <Outlet />
8 </div>
9 );
10}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What component in React Router v6 automatically emulates the browser's native scroll behavior across SPA route transitions?
Question 2
Where should <ScrollRestoration /> be rendered in an application?
Question 3
What hook allows components to detect when an asynchronous route loader or form submission is currently in-flight?
Question 4
How can you create a top-of-screen progress bar (similar to GitHub or YouTube) during route changes?
Question 5
What does the getKey prop on <ScrollRestoration> allow developers to customize?
Next Lesson

Architecture of Server-Side Rendering vs Client-Side Rendering

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Protected Routes and Role-Based Access GuardsArchitecture of Server-Side Rendering vs Client-Side Rendering

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext