0
0
Reactframework~10 mins

Defining routes in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defining routes
Start App
Define Routes
User visits URL
Router matches URL to Route
Render matched Component
Display Component on screen
The app starts by defining routes. When a user visits a URL, the router matches it to a route and renders the correct component.
Execution Sample
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>
  );
}

export default App;
This code sets up two routes: '/' renders Home component, '/about' renders About component.
Execution Table
StepUser URLRouter ActionMatched RouteComponent Rendered
1/Check routespath='/'Home
2/aboutCheck routespath='/about'About
3/contactCheck routesNo matchNo component (404 or fallback)
4ExitNo more URLs--
💡 No more URLs to match, routing ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
User URL'''/''/about''/contact'No more URLs
Matched Routenull'/''/about'nullnull
Component RenderednullHomeAboutNo component (404 or fallback)No component (404 or fallback)
Key Moments - 2 Insights
Why does the router render no component for '/contact'?
Because '/contact' does not match any defined route path, so no component is rendered as shown in execution_table row 3.
How does the router decide which component to render?
It compares the user URL with each route's path and renders the component of the first matching route, as seen in execution_table rows 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what component is rendered when the user visits '/'?
AHome
BAbout
CContact
DNone
💡 Hint
Check execution_table row 1 under 'Component Rendered'
At which step does the router find no matching route?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table row 3 under 'Matched Route'
If we add a route for '/contact', how would the component rendered change at step 3?
AIt would render Home
BIt would render About
CIt would render Contact component
DIt would still render no component
💡 Hint
Refer to variable_tracker for 'Component Rendered' at step 3 and imagine adding a matching route
Concept Snapshot
Defining routes in React uses <BrowserRouter> to wrap <Routes>.
Inside <Routes>, each <Route> has a path and an element (component).
When user visits a URL, router matches path and renders the element.
No match means no component rendered (can show 404).
Routes must be inside <BrowserRouter> for routing to work.
Full Transcript
This visual execution shows how React routing works by defining routes inside BrowserRouter. When the app starts, routes are set with paths and components. When a user visits a URL, the router checks each route to find a match. If a match is found, it renders the corresponding component. If no match is found, no component is rendered. The execution table traces user URLs and router actions step-by-step. Variable tracker shows how user URL, matched route, and rendered component change over time. Key moments clarify why no component renders for unmatched URLs and how matching works. The quiz tests understanding of route matching and rendering behavior.