 Route-Based Rendering
Route-Based Rendering
We can render components based on URLs. To do that, we need to use the following components:
- We use BrowserRouterto enclose all the other code in the main React component;
- Routesis used for enclosing all individual- Routecomponents and their child components. It returns a single React component based on the URL. There can be multiple- <Routes>components in the code;
- Routerepresents a URL path and points to a React component.
Look at the example from the previous chapter:
<BrowserRouter>
  <Routes>
    <Route path="/" element={<Home />} />
    <Route path="about" element={<About />} />
    <Route path="*" element={<Error />} />
  </Routes>
</BrowserRouter>
- The <Routes>component groups all the<Route>components and returns the most relevant React component based on the current user's URL path;
- The <Route>component has two attributes:pathandelement. Thepathattribute is the relative path based on the parent component. It will make more sense with further explanation. Theelementstores a reference to the React component that the URL should render;
- We can nest <Route>components to create nested routes. For example, to createhttp://***.com/about/updatespath, we can change the structure like this:
<BrowserRouter>
  <Routes>
    <Route path="/" element={<Home />} />
    <Route path="about">
      <Route index element={<About />} />
      <Route path="updates" element={<Updates />} />
    </Route>
    <Route path="*" element={<Error />} />
  </Routes>
</BrowserRouter>
In the above code, we created an about path but didn't specify a target component. Instead, we created two nested routes.
The first nested Route doesn't have a path attribute but, instead, an index, which indicates that its path is that of the parent Route.
The other one points to the Update component having path updates - React Router automatically considers this path relative to the parent path, that is, http://***.com/about/.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Posez-moi des questions sur ce sujet
Résumer ce chapitre
Afficher des exemples du monde réel
Awesome!
Completion rate improved to 2.7 Route-Based Rendering
Route-Based Rendering
Glissez pour afficher le menu
We can render components based on URLs. To do that, we need to use the following components:
- We use BrowserRouterto enclose all the other code in the main React component;
- Routesis used for enclosing all individual- Routecomponents and their child components. It returns a single React component based on the URL. There can be multiple- <Routes>components in the code;
- Routerepresents a URL path and points to a React component.
Look at the example from the previous chapter:
<BrowserRouter>
  <Routes>
    <Route path="/" element={<Home />} />
    <Route path="about" element={<About />} />
    <Route path="*" element={<Error />} />
  </Routes>
</BrowserRouter>
- The <Routes>component groups all the<Route>components and returns the most relevant React component based on the current user's URL path;
- The <Route>component has two attributes:pathandelement. Thepathattribute is the relative path based on the parent component. It will make more sense with further explanation. Theelementstores a reference to the React component that the URL should render;
- We can nest <Route>components to create nested routes. For example, to createhttp://***.com/about/updatespath, we can change the structure like this:
<BrowserRouter>
  <Routes>
    <Route path="/" element={<Home />} />
    <Route path="about">
      <Route index element={<About />} />
      <Route path="updates" element={<Updates />} />
    </Route>
    <Route path="*" element={<Error />} />
  </Routes>
</BrowserRouter>
In the above code, we created an about path but didn't specify a target component. Instead, we created two nested routes.
The first nested Route doesn't have a path attribute but, instead, an index, which indicates that its path is that of the parent Route.
The other one points to the Update component having path updates - React Router automatically considers this path relative to the parent path, that is, http://***.com/about/.
Merci pour vos commentaires !