Summary - Router basics
- React course
- Intermediate
- Lesson 40/80
- 5 min
Set up a two-route skeleton with home and about pages.
Summary - Router basics
In this group you learned what React Router is, defining routes, and navigation with Link/NavLink. Now you can build basic SPAs.
The recipe: wrap with BrowserRouter, define Route inside Routes, build the menu with NavLink. Add a path="*" route for 404. These four steps set up a working router.
In the next step you'll write an App component combining NavBar + Routes.
Practice App router + navbar
Complete the skeleton. Write an App component. Inside <BrowserRouter>, first <NavBar />, then inside <Routes> define two routes: <Route path="/" element={ '{ <Home /> }' } />, <Route path="/about" element={ '{ <About /> }' } />. Export it. (Assume all components are imported.)
Starter code
// Write the App component hereSample solution
function App() {
return (
<BrowserRouter>
<NavBar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
}
export default App;Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which reloads the page?
- <a href> Correct answer
- <Link to>
- <NavLink to>
- <Route>
Explanation: a href reloads the page.
2 Match the router recipe
| Term | Meaning |
|---|---|
BrowserRouter |
wraps |
Routes |
container |
Route |
route |
NavLink |
menu |