CodeBlanks Logo Icon CodeBlanks

Summary - Router basics

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

App.jsx
// Write the App component here

Sample solution

App.jsx
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

All lessons in Intermediate