Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Defining Routes for Navigation | Implementing React Router in a React App
React Router Essentials
course content

Зміст курсу

React Router Essentials

React Router Essentials

1. Understanding React Router and URL Structures
2. Implementing React Router in a React App
3. Advanced React Router Features

book
Defining Routes for Navigation

Now that we've wrapped our app with BrowserRouter to enable routing, the next step is to specify the routes (paths) our app will include. These routes determine the overall structure of the app and control what content is displayed based on the URL.

React Router provides two essential components for defining routes: Routes and Route. The Routes component acts as a container for all route definitions, while the Route component represents a specific route configuration.

Here's the syntax for defining routes:

In this structure:

  • <path_value>: Specifies the URL configuration for the route;
  • <component_to_render>: Determines the component to render when a user navigates to that specific path.

Example

Let's apply this concept to our project. Open the App.jsx file and import the Routes and Route components from the react-router-dom library:

Within the return statement of the App component, define the routes for our app:

Code explanation:

  • Line 4, 8: <Routes>: This component serves as a container for defining routes in the application, acting as a parent component for individual route definitions;
  • Line 5: <Route>: Represents a route definition. It specifies that when the URL path matches /, the component <HomePage /> should be rendered;
  • Line 6: <Route>: Defines another route. When the URL path matches /about, the component <AboutPage /> will be rendered;
  • Line 7: <Route>: Defines another route. When the URL path matches /contacts, the <ContactsPage /> component will be rendered.

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4
We're sorry to hear that something went wrong. What happened?
some-alt