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.
Single Page Application concept in 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.
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> ); }
Link instead of <a> tags to navigate inside an SPA without full page reload.import { Link } from 'react-router-dom'; function Navigation() { return ( <nav> <Link to="/">Home</Link> <Link to="/about">About</Link> </nav> ); }
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.
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> ); }
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.
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
Solution
Step 1: Understand SPA behavior
SPAs load a single HTML page and dynamically update content without reloading the entire page.Step 2: Compare with traditional websites
Traditional sites reload the whole page on navigation, causing slower user experience.Final Answer:
It loads the page once and updates content dynamically without full reloads. -> Option BQuick Check:
SPA = dynamic content update without reload [OK]
- Thinking SPAs reload pages on every action
- Confusing SPAs with server-only rendering
- Believing SPAs don't support navigation
Solution
Step 1: Identify navigation management in React
React Router is designed to handle routing and URL paths in SPAs.Step 2: Differentiate other libraries
Redux manages state, Axios handles HTTP requests, Styled Components manage styling.Final Answer:
React Router -> Option DQuick Check:
Routing in React SPA = React Router [OK]
- Confusing Redux with routing
- Thinking Axios manages navigation
- Assuming styling libraries handle routing
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?Solution
Step 1: Analyze React Router setup
TheRoutewith path '/about' renders theAboutcomponent insideBrowserRouter.Step 2: Understand SPA navigation
React Router updates the view without reloading the whole page when navigating to '/about'.Final Answer:
The About component renders without a full page reload. -> Option CQuick Check:
React Router navigation = no full reload [OK]
- Assuming full page reload on route change
- Thinking routes are not matched correctly
- Confusing Home and About components
import { BrowserRouter, Routes, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Routes>
<Route path='/' component={Home} />
<Route path='/profile' element=<Profile /> />
</Routes>
</BrowserRouter>
);
}Solution
Step 1: Check Route props
React Router v6 uses 'element' prop with JSX, not 'component'.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.Final Answer:
The first Route uses 'component' prop instead of 'element'. -> Option AQuick Check:
React Router v6 Route uses element prop [OK]
- Using 'component' prop from older React Router versions
- Confusing Routes with Switch in v6
- Assuming missing closing tags without checking
Solution
Step 1: Understand SPA data fetching
SPAs fetch data asynchronously and update views without full reloads.Step 2: Implement loading state in route components
Showing a spinner while fetching data fits SPA dynamic update and user feedback.Step 3: Eliminate non-SPA approaches
Reloading pages or using multiple HTML pages breaks SPA flow; fetching once without updates is inflexible.Final Answer:
Use React Router for routes and show a spinner component while fetching data asynchronously in the route component. -> Option AQuick Check:
SPA + async data + spinner = Use React Router for routes and show a spinner component while fetching data asynchronously in the route component. [OK]
- Reloading pages instead of dynamic updates
- Not showing loading feedback during fetch
- Ignoring SPA dynamic data needs
Ready to prove it?
Pass the test and this pattern moves unknown
