Build a Simple Single Page Application (SPA) with React
📖 Scenario: You want to create a small website that shows different content without reloading the page. This is called a Single Page Application (SPA). You will use React to build it.
🎯 Goal: Build a React SPA with two buttons to switch between two views: Home and About. Clicking a button changes the displayed content without reloading the page.
📋 What You'll Learn
Create a React functional component called App.
Use useState to track the current page.
Add two buttons labeled Home and About.
Show different text depending on the selected page.
Switch content instantly when clicking buttons without page reload.
💡 Why This Matters
🌍 Real World
Single Page Applications are common for websites that want fast, smooth user experiences without full page reloads. React is a popular tool to build SPAs.
💼 Career
Understanding SPA basics with React is essential for frontend developer roles, enabling you to build modern interactive web apps.
Progress0 / 4 steps
1
Set up the React component and initial state
Create a React functional component called App. Inside it, use useState to create a state variable named page with the initial value 'home'.
React
Hint
Remember to import useState from React and call it inside your App function.
2
Add buttons to switch pages
Inside the App component, add two <button> elements labeled Home and About. Add onClick handlers to these buttons that call setPage with 'home' and 'about' respectively.
React
Hint
Use arrow functions inside onClick to call setPage with the correct page name.
3
Display content based on the current page
Below the buttons, add a conditional rendering that shows <p>Welcome to the Home page</p> when page is 'home', and <p>This is the About page</p> when page is 'about'. Use a JavaScript if or ternary expression inside the JSX.
React
Hint
Use a ternary operator inside curly braces to show different paragraphs based on page.
4
Add basic accessibility and semantic HTML
Wrap the buttons inside a <nav> element with an aria-label of 'Main navigation'. Wrap the content paragraph inside a <main> element.
React
Hint
Use semantic HTML tags to improve accessibility and structure.
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
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 B
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
Step 1: Identify navigation management in React
React Router is designed to handle routing and URL paths in SPAs.