Bird
Raised Fist0
Reactframework~5 mins

Single Page Application concept in React

Choose your learning style10 modes available
Introduction

A Single Page Application (SPA) loads one main page and updates content dynamically without reloading the whole page. This makes apps feel faster and smoother, like a desktop app.

Building a website that feels like a mobile app with quick interactions.
Creating a dashboard where users switch views without waiting for full page reloads.
Making a blog or portfolio where content updates without refreshing the page.
Developing an online store where users browse products and add to cart instantly.
Building a chat app where messages update live without page reload.
Syntax
React
import React from 'react';
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';

function Home() {
  return <h2>Home Page</h2>;
}

function About() {
  return <h2>About Page</h2>;
}

export default function App() {
  return (
    <BrowserRouter>
      <nav>
        <Link to="/">Home</Link> | <Link to="/about">About</Link>
      </nav>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}

Use BrowserRouter to enable SPA routing in React.

Routes and Route define which component shows for each URL path.

Examples
This example shows two pages: Home and Contact. Clicking links changes content without reloading the page.
React
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function Home() {
  return <h1>Welcome Home</h1>;
}

function Contact() {
  return <h1>Contact Us</h1>;
}

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/contact" element={<Contact />} />
      </Routes>
    </BrowserRouter>
  );
}
Use Link instead of <a> tags to navigate inside an SPA without full page reload.
React
import { Link } from 'react-router-dom';

function Navigation() {
  return (
    <nav>
      <Link to="/">Home</Link>
      <Link to="/about">About</Link>
    </nav>
  );
}
Sample Program

This React app uses SPA routing. Clicking "Home" or "About" changes the content below without reloading the page. The navigation uses semantic HTML and an ARIA label for accessibility.

React
import React from 'react';
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';

function Home() {
  return <h2>Home Page</h2>;
}

function About() {
  return <h2>About Page</h2>;
}

export default function App() {
  return (
    <BrowserRouter>
      <header>
        <nav aria-label="Main navigation">
          <Link to="/" style={{ marginRight: '1rem' }}>Home</Link>
          <Link to="/about">About</Link>
        </nav>
      </header>
      <main>
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/about" element={<About />} />
        </Routes>
      </main>
    </BrowserRouter>
  );
}
OutputSuccess
Important Notes

SPAs improve user experience by avoiding full page reloads.

Use React Router's Link to navigate inside the app.

Remember to add semantic HTML and ARIA labels for accessibility.

Summary

Single Page Applications load one page and update content dynamically.

React Router helps build SPAs by managing URL paths and components.

SPAs feel faster and smoother because they avoid full page reloads.

Practice

(1/5)
1. What is the main advantage of a Single Page Application (SPA) compared to traditional multi-page websites?
easy
A. It uses only server-side rendering without JavaScript.
B. It loads the page once and updates content dynamically without full reloads.
C. It requires a new page load for every user action.
D. It does not support navigation between different views.

Solution

  1. Step 1: Understand SPA behavior

    SPAs load a single HTML page and dynamically update content without reloading the entire page.
  2. Step 2: Compare with traditional websites

    Traditional sites reload the whole page on navigation, causing slower user experience.
  3. Final Answer:

    It loads the page once and updates content dynamically without full reloads. -> Option B
  4. Quick Check:

    SPA = dynamic content update without reload [OK]
Hint: SPAs avoid full page reloads for faster user experience [OK]
Common Mistakes:
  • Thinking SPAs reload pages on every action
  • Confusing SPAs with server-only rendering
  • Believing SPAs don't support navigation
2. Which React library is commonly used to manage navigation and URL paths in a Single Page Application?
easy
A. Styled Components
B. Redux
C. Axios
D. React Router

Solution

  1. Step 1: Identify navigation management in React

    React Router is designed to handle routing and URL paths in SPAs.
  2. Step 2: Differentiate other libraries

    Redux manages state, Axios handles HTTP requests, Styled Components manage styling.
  3. Final Answer:

    React Router -> Option D
  4. Quick Check:

    Routing in React SPA = React Router [OK]
Hint: React Router controls SPA navigation and URLs [OK]
Common Mistakes:
  • Confusing Redux with routing
  • Thinking Axios manages navigation
  • Assuming styling libraries handle routing
3. Given this React SPA code snippet using React Router:
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path='/' element=<Home /> />
        <Route path='/about' element=<About /> />
      </Routes>
    </BrowserRouter>
  );
}

What happens when the user navigates to /about URL?
medium
A. Nothing happens because routes are not defined correctly.
B. The browser reloads the entire page and shows About component.
C. The About component renders without a full page reload.
D. The Home component renders instead of About.

Solution

  1. Step 1: Analyze React Router setup

    The Route with path '/about' renders the About component inside BrowserRouter.
  2. Step 2: Understand SPA navigation

    React Router updates the view without reloading the whole page when navigating to '/about'.
  3. Final Answer:

    The About component renders without a full page reload. -> Option C
  4. Quick Check:

    React Router navigation = no full reload [OK]
Hint: React Router changes views without reloading page [OK]
Common Mistakes:
  • Assuming full page reload on route change
  • Thinking routes are not matched correctly
  • Confusing Home and About components
4. Identify the error in this React SPA routing code:
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path='/' component={Home} />
        <Route path='/profile' element=<Profile /> />
      </Routes>
    </BrowserRouter>
  );
}
medium
A. The first Route uses 'component' prop instead of 'element'.
B. The BrowserRouter is missing a closing tag.
C. Routes should be replaced with Switch.
D. Profile component is not imported.

Solution

  1. Step 1: Check Route props

    React Router v6 uses 'element' prop with JSX, not 'component'.
  2. Step 2: Verify other code parts

    BrowserRouter is properly closed, Routes is correct in v6, and Profile import is not shown but not necessarily an error here.
  3. Final Answer:

    The first Route uses 'component' prop instead of 'element'. -> Option A
  4. Quick Check:

    React Router v6 Route uses element prop [OK]
Hint: Use 'element' prop with JSX in React Router v6 [OK]
Common Mistakes:
  • Using 'component' prop from older React Router versions
  • Confusing Routes with Switch in v6
  • Assuming missing closing tags without checking
5. You want to build a React SPA that shows a loading spinner while fetching user data on route change. Which approach best fits SPA principles?
hard
A. Use React Router for routes and show a spinner component while fetching data asynchronously in the route component.
B. Reload the entire page on each route change to fetch fresh data.
C. Fetch all user data once on app load and never update it.
D. Use multiple HTML pages and link between them for each user view.

Solution

  1. Step 1: Understand SPA data fetching

    SPAs fetch data asynchronously and update views without full reloads.
  2. Step 2: Implement loading state in route components

    Showing a spinner while fetching data fits SPA dynamic update and user feedback.
  3. Step 3: Eliminate non-SPA approaches

    Reloading pages or using multiple HTML pages breaks SPA flow; fetching once without updates is inflexible.
  4. Final Answer:

    Use React Router for routes and show a spinner component while fetching data asynchronously in the route component. -> Option A
  5. Quick Check:

    SPA + async data + spinner = Use React Router for routes and show a spinner component while fetching data asynchronously in the route component. [OK]
Hint: Show spinner during async fetch inside SPA route components [OK]
Common Mistakes:
  • Reloading pages instead of dynamic updates
  • Not showing loading feedback during fetch
  • Ignoring SPA dynamic data needs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard