Bird
Raised Fist0
Reactframework~5 mins

Defining routes in React

Choose your learning style10 modes available
Introduction

Routes help your app show different pages or views when users click links or enter URLs. They make your app feel like a website with many pages.

You want to show a home page and an about page in your app.
You need to switch views without reloading the whole page.
You want users to bookmark or share links to specific parts of your app.
You want to protect some pages so only logged-in users can see them.
Syntax
React
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}

BrowserRouter wraps your app to enable routing.

Routes holds all your route definitions.

Examples
Defines two routes: home page at '/' and contact page at '/contact'.
React
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/contact" element={<Contact />} />
      </Routes>
    </BrowserRouter>
  );
}
Route with a dynamic parameter userId to show different profiles.
React
<Route path="/profile/:userId" element={<Profile />} />
Catch-all route to show a 'Not Found' page for unknown URLs.
React
<Route path="*" element={<NotFound />} />
Sample Program

This React app uses routes to switch between Home and About pages. The navigation links let users change pages without reloading. The aria-label helps screen readers understand the menu.

React
import React from 'react';
import { createRoot } from 'react-dom/client';
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';

function Home() {
  return <h2>Home Page</h2>;
}

function About() {
  return <h2>About Page</h2>;
}

function App() {
  return (
    <BrowserRouter>
      <nav aria-label="Main navigation">
        <ul>
          <li><Link to="/">Home</Link></li>
          <li><Link to="/about">About</Link></li>
        </ul>
      </nav>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}

const container = document.getElementById('root');
const root = createRoot(container);
root.render(<App />);
OutputSuccess
Important Notes

Always wrap your routes inside BrowserRouter to enable routing.

Use Link instead of <a> tags to avoid full page reloads.

Route paths are case sensitive by default.

Summary

Routes let your React app show different pages without reloading.

Use BrowserRouter, Routes, and Route to define routes.

Use Link to navigate between routes smoothly.

Practice

(1/5)
1. Which component is used to wrap your entire React app to enable routing?
easy
A. Link
B. Route
C. BrowserRouter
D. Routes

Solution

  1. Step 1: Understand routing setup

    React Router requires a top-level component to manage routing context.
  2. Step 2: Identify the wrapper component

    BrowserRouter wraps the app to enable routing features.
  3. Final Answer:

    <BrowserRouter> -> Option C
  4. Quick Check:

    Routing wrapper = BrowserRouter [OK]
Hint: Remember: BrowserRouter wraps the whole app for routing [OK]
Common Mistakes:
  • Using <Routes> as the top wrapper
  • Confusing <Link> with the router wrapper
  • Using <Route> to wrap the app
2. Which of the following is the correct syntax to define a route for path '/home' rendering component?
easy
A. } />
B.
C. } />
D. } />

Solution

  1. Step 1: Recall Route syntax in React Router v6+

    Routes use path for URL and element for component to render.
  2. Step 2: Match correct syntax

    <Route path='/home' element={<Home />} /> uses path='/home' and element={<Home />}, which is correct.
  3. Final Answer:

    <Route path='/home' element={<Home />} /> -> Option A
  4. Quick Check:

    Route syntax = path + element [OK]
Hint: Use path for URL and element for component in Route [OK]
Common Mistakes:
  • Using component instead of element
  • Using to instead of path
  • Passing component as JSX instead of element prop
3. Given this code snippet, what will be rendered when the URL path is '/about'?
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path='/' element={

Home

} /> <Route path='/about' element={

About Us

} /> <Route path='/contact' element={

Contact

} /> </Routes> </BrowserRouter> ); }
medium
A.

Home

B. Nothing renders
C.

Contact

D.

About Us

Solution

  1. Step 1: Match URL path to Route path

    The URL '/about' matches the Route with path='/about'.
  2. Step 2: Identify rendered element

    The matched Route renders <h1>About Us</h1>.
  3. Final Answer:

    <h1>About Us</h1> -> Option D
  4. Quick Check:

    URL '/about' renders About Us [OK]
Hint: Match URL path exactly to Route path to find output [OK]
Common Mistakes:
  • Confusing '/' with '/about'
  • Assuming default route renders for all paths
  • Ignoring exact path matching
4. What is wrong with this route definition?
<Route path='/profile' component={} />
medium
A. The path should be '/profiles' instead of '/profile'
B. The prop 'component' should be 'element' with JSX
C. The component should be passed as a string, not JSX
D. Route cannot have a path prop

Solution

  1. Step 1: Recall correct Route props in React Router v6+

    Routes use element prop to pass JSX, not component.
  2. Step 2: Identify the error

    Using component causes the route not to render correctly.
  3. Final Answer:

    The prop 'component' should be 'element' with JSX -> Option B
  4. Quick Check:

    Use element, not component, for Route JSX [OK]
Hint: Use element prop with JSX in Route, not component [OK]
Common Mistakes:
  • Using component prop from older React Router versions
  • Passing component as string
  • Omitting element or component prop
5. You want to create nested routes where '/dashboard' shows a component and '/dashboard/settings' shows inside it. Which is the correct way to define these routes?
hard
A. }> } />
B. } /> } />
C. }> } />
D. }> } />

Solution

  1. Step 1: Understand nested route path rules

    Nested routes use relative paths inside parent Route; child path 'settings' appends to '/dashboard'.
  2. Step 2: Check each option for correct nesting

    <Routes> <Route path='/dashboard' element={<Dashboard />}> <Route path='settings' element={<Settings />} /> </Route> </Routes> correctly nests settings as child path without repeating parent path.
  3. Final Answer:

    <Routes> <Route path='/dashboard' element={<Dashboard />}> <Route path='settings' element={<Settings />} /> </Route> </Routes> -> Option A
  4. Quick Check:

    Nested child paths are relative, no leading slash [OK]
Hint: Nested routes use relative paths without leading slash [OK]
Common Mistakes:
  • Using absolute paths in nested routes
  • Defining nested routes as siblings
  • Repeating parent path in child route
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard