0
0
Reactframework~5 mins

Single Page Application concept in React

Choose your learning style9 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.