Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Defining Routes | Basic React Router Concepts
React Router
course content

Conteúdo do Curso

React Router

React Router

1. Conceptual Overview
2. Basic React Router Concepts
3. Advanced React Router Concepts

bookDefining Routes

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.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4
some-alt