React Router Tutorial - 3 - Configuring Routes

Codevolution
6 Jan 202207:52

Summary

TLDRThis tutorial video teaches how to set up routes using React Router for a simple web application. It covers configuring the home and about pages, wrapping the app with the Browser Router, and creating separate components for each route. The script also hints at future lessons on user navigation through UI elements and programmatic route changes, enticing viewers to stay tuned for more.

Takeaways

  • πŸš€ The video teaches how to configure routes with React Router for a web application.
  • 🌐 Two main routes are set up: the home route and the about route, corresponding to the root and '/about' URL paths.
  • πŸ“ The 'index.js' file is where the app component is initially rendered to the DOM.
  • 🏠 The home route is rendered when the user navigates to 'localhost:3000'.
  • πŸ“– The about route is rendered when the user navigates to 'localhost:3000/about'.
  • πŸ”„ To configure routing, the 'BrowserRouter' from 'react-router-dom' is used to wrap the entire app.
  • πŸ› οΈ A new 'components' folder is created with 'Home.js' and 'About.js' files for the respective route views.
  • πŸ“‚ 'Home.js' and 'About.js' are simple functional components rendering text for their respective pages.
  • πŸ”‘ The 'Routes' and 'Route' components from 'react-router-dom' are used to define the path and element for each route.
  • πŸ”„ The 'path' prop in the 'Route' component is used to match the URL path, while the 'element' prop specifies the component to render.
  • πŸ”— The video concludes with a summary of the steps taken to configure routing and a teaser for the next video on navigating routes using UI elements.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is how to configure routes with React Router in a React application.

  • What are the two routes set up in the video scenario?

    -The two routes set up are the home route, which is accessible at localhost port 3000, and the about route, accessible at localhost port 3000/slash/about.

  • What is the purpose of the BrowserRouter component from React Router?

    -The BrowserRouter component is used to connect the URL in the browser with the React application, allowing the use of all React Router features within the app component tree.

  • What is the first step to configuring routes with React Router as described in the video?

    -The first step is to wrap the root component of the app with the BrowserRouter component from the React Router package.

  • Where is the app component rendered to the DOM in the given scenario?

    -The app component is rendered to the DOM in the index.js file.

  • What components need to be created for the app root and '/about' URL in the video?

    -Two components need to be created: Home.js for the app root and About.js for the '/about' URL.

  • What are the two props specified on the Route component in the video?

    -The two props specified on the Route component are 'path', which reflects the path in the URL, and 'element', which is the component to be rendered when the path is matched.

  • How can users navigate to different routes in a typical web application?

    -Users can navigate to different routes by clicking on UI elements like links or by being programmatically navigated to a different route after an action has been completed.

  • What does the video suggest will be covered in the next video?

    -The next video will cover navigating to different routes using an element in the UI with React Router.

  • What is the final outcome of the route configuration in the video?

    -The final outcome is that the home component is viewable at localhost port 3000 and the about component is viewable at localhost port 3000/about, with the routes working as expected.

Outlines

00:00

πŸ“š Configuring Basic Routes with React Router

This paragraph introduces the process of setting up routes using React Router for a simple application with two main pages: Home and About. The tutorial begins by explaining the need to connect the browser's URL with the React application using the 'BrowserRouter' component. It then details the creation of two new components, 'Home' and 'About', which will represent the respective pages. The 'Routes' and 'Route' components from React Router are imported and utilized to define the paths for these components. The 'path' prop is used to match the URL path, while the 'element' prop specifies which component to render. The paragraph concludes by showing the application of these routes in the browser, with the 'Home' page displayed at the root URL and the 'About' page at the '/about' path.

05:02

πŸ”— Understanding Navigation in React Router

The second paragraph delves into the navigation aspect of React Router, explaining that while the previous setup allows for URL-based navigation, it's not the typical user experience. It highlights the need for UI elements like links for user interaction and programmatic navigation post-actions. The summary recaps the steps taken so far: wrapping the root component with 'BrowserRouter', creating view components for different URL paths, and configuring routes with 'Routes' and 'Route'. It emphasizes the current navigation method's limitations and hints at the next tutorial's focus on navigating routes using UI elements and programmatically, promising to explore these methods in more detail.

Mindmap

Keywords

πŸ’‘React Router

React Router is a standard library for routing in React applications. It allows developers to define navigation paths and associate them with specific components, enabling a single-page application (SPA) to handle multiple views without page reloads. In the video, React Router is used to configure routes for a home page and an about page, demonstrating its core functionality in a web development context.

πŸ’‘Routes

In the context of web development, 'routes' refer to the paths used to access different pages or resources in an application. The script discusses setting up two routes: the home route and the about route. Each route is associated with a specific URL path and a React component that renders when that path is accessed.

πŸ’‘Home Route

The 'home route' is the default route of an application, typically accessed by navigating to the base URL (e.g., 'localhost:3000'). In the video, the home route is configured to display a 'home page' when the user visits the root URL of the application, illustrating the basic usage of routing in React.

πŸ’‘About Route

The 'about route' is a specific path in the application that leads to an 'about' page, usually accessed by appending '/about' to the base URL. In the script, configuring the about route involves setting up a path that, when visited, renders the 'about' component, showing how to create and link secondary pages in a React application.

πŸ’‘Browser Router

Browser Router is a component provided by React Router to manage the browser's history using the HTML5 history API. It wraps the entire application and allows the use of navigation features within the app component tree. In the video, Browser Router is used to connect the application's UI with the browser's URL, enabling navigation between different routes.

πŸ’‘Component

In React, a 'component' is a modular piece of code that represents a part of the user interface. The script describes creating 'home' and 'about' components, which are simple functional components that render text. These components are then associated with specific routes to display their content when the corresponding URLs are accessed.

πŸ’‘Path Prop

The 'path prop' is a property used within the 'Route' component in React Router to specify the URL path that triggers the rendering of a component. In the video, the path prop is set to '/' for the home route and '/about' for the about route, defining the URLs that will display the respective components.

πŸ’‘Element Prop

The 'element prop' in the 'Route' component is used to specify the React component that should be rendered when the route's path matches the current URL. In the script, the element prop is assigned the 'home' component for the root path and the 'about' component for the '/about' path, demonstrating how to map components to routes.

πŸ’‘UI Element

A 'UI element' refers to any interactive component in a graphical user interface, such as buttons or links. The script mentions UI elements in the context of navigation, where users can click on links to navigate to different routes within the application, providing a more dynamic user experience than manually entering URLs.

πŸ’‘Programmatic Navigation

Programmatic navigation is the process of changing the route or view in an application through code, rather than user-initiated actions like clicking a link. The video script hints at covering this concept in a follow-up video, where it would likely involve using React Router's navigation functions to redirect the user to a different route based on certain actions or events.

Highlights

Introduction to configuring routes with React Router for a web application.

Setting up two routes: home and about for a basic React application.

Explanation of the default behavior when navigating to the root URL.

Demonstration of viewing the about page by accessing a specific URL.

Overview of the current application structure with `index.js` and `app.js`.

Importing and using `BrowserRouter` to connect the browser URL with the React application.

Wrapping the entire app with `BrowserRouter` to enable route features.

Creating new component files for the home and about pages.

Configuring routes by importing `Routes` and `Route` from React Router DOM.

Defining the root route with a path of '/' and rendering the `Home` component.

Setting up the about route with a path of '/about' and rendering the `About` component.

Ensuring components are imported for proper route rendering.

Visual confirmation of route functionality in the browser.

Summary of the steps involved in configuring routes with React Router.

Teaser for the next video on navigating routes using UI elements.

Encouragement for viewers to subscribe for more content.

Transcripts

play00:04

welcome back

play00:06

in this video we're going to learn how

play00:08

to configure routes with react router

play00:12

for our scenario we're going to set up

play00:14

two routes the first route is the home

play00:18

route

play00:19

if the user navigates to localhost port

play00:21

3000 they should be able to see the home

play00:24

page and if the visit localhost 3000

play00:28

slash about

play00:29

they should be able to see the about

play00:31

page

play00:33

let's head over to es code and

play00:35

understand how to achieve this with

play00:36

react router

play00:39

at the moment in the source folder we

play00:41

have index.js

play00:44

where we render the app component to the

play00:46

dom

play00:47

the app component is present in app.js

play00:51

and contains a very simple ui

play00:54

react logo and some text

play00:58

if we take a look at the browser

play01:00

we should be able to see the same being

play01:02

rendered

play01:05

now the first step to configuring routes

play01:07

with react router is to connect the url

play01:11

in the browser with our react

play01:13

application

play01:15

for that react router provides a

play01:17

component called browser router with

play01:20

which we need to wrap our entire app

play01:23

so in index.js

play01:26

at the top

play01:28

import

play01:30

from react router dom

play01:34

a component called browser router

play01:38

next wrap the app component with browser

play01:42

router

play01:47

what this allows us to do is use all the

play01:50

features from react router within the

play01:53

app component tree

play01:55

let's head over to app.js and configure

play01:57

the routes

play01:59

i'm going to begin by removing the

play02:01

components jsx in its entirety and also

play02:05

the import statements

play02:10

but before we configure the routes we

play02:12

need the components that need to be

play02:14

rendered for the app root and slash

play02:17

about in the url

play02:20

so in the source folder i'm going to

play02:22

create a new folder called components

play02:26

within this folder i'm going to create

play02:28

two files

play02:30

home dot js

play02:33

and

play02:35

about dot js

play02:39

within home.js create a very simple

play02:42

function component

play02:46

that renders the text home

play02:48

page do the same in about.js so copy

play02:53

paste and change home to about

play02:58

now that we have the two views

play03:01

let's configure the

play03:02

routes for the route configuration we

play03:06

need two components from react router

play03:09

so in app.js at the top

play03:12

import

play03:14

routes and route

play03:16

from

play03:17

react router dom

play03:20

in the component jsx add the routes

play03:22

component

play03:25

within the routes component we defined

play03:28

the individual route using the route

play03:30

component

play03:35

on the route component we specify two

play03:38

props

play03:39

the first prop is path

play03:44

which reflects the path in the url

play03:48

our first route is the root of the app

play03:51

which is localhost port 3000

play03:54

and that is denoted by a forward slash

play03:58

so the value to the path prop is a

play04:00

forward slash

play04:03

we then tell react router

play04:05

what is the element that needs to be

play04:07

rendered when the url matches this path

play04:11

in our case it would be the home

play04:13

component

play04:14

so we add the second prop which is

play04:17

element

play04:18

and to this we assign the home component

play04:23

make sure to import home component at

play04:26

the top if we now head back to the

play04:29

browser

play04:32

we should see the text home page being

play04:34

rendered

play04:35

the gsx from home component

play04:39

our first route is working as expected

play04:42

let's configure the second route now

play04:48

invoke the route component once again

play04:51

we're going to set path

play04:52

to about

play04:55

and element

play04:57

is going to be equal to the about

play04:59

component

play05:02

make sure to import it at the top

play05:05

so we are asking react router to render

play05:08

the about component

play05:10

when the path in the url is about

play05:14

of course in both these cases route can

play05:16

be a self-closing tag

play05:19

if we head back to the browser

play05:23

and type localhost port 3000

play05:26

slash

play05:27

about

play05:29

you can see the about page being

play05:32

rendered

play05:33

our route configuration is working as

play05:36

expected

play05:39

this is pretty much how you configure

play05:42

routes with react router

play05:44

let me quickly summarize the steps

play05:47

step one

play05:48

we wrap the root component of the app

play05:51

with browser router from the react

play05:53

router package

play05:55

in our case we wrapped the app component

play05:58

with browser router

play06:00

step 2 create the components that need

play06:03

to be rendered at different url parts

play06:06

we created the home and about components

play06:10

step 3

play06:12

configure the routes using the routes

play06:15

and route components from react router

play06:20

routes at the top level which can

play06:22

contain multiple route components

play06:26

each route will accept a path prop which

play06:29

corresponds to the path in the browser

play06:31

url

play06:32

and the corresponding react element to

play06:35

render when the path is matched

play06:38

we have set home component for root of

play06:41

the app

play06:42

and about component for the about path

play06:47

this lets us view the home component at

play06:50

localhost port 3000 and the about

play06:53

component at localhost port 3000 slash

play06:57

about

play06:59

but what you might have noticed is that

play07:01

we navigate to the different pages by

play07:04

entering the url in the browser address

play07:06

bar

play07:08

this of course is not how a regular user

play07:11

would navigate in a web application

play07:14

typically we have a ui element like a

play07:17

link which the user can click to

play07:19

navigate to a different route or the

play07:22

user could also be navigated

play07:24

programmatically to a different route

play07:26

after an action has completed for

play07:28

example

play07:30

let's learn how to do the same with

play07:32

react router

play07:34

in the next video let's understand

play07:36

navigating to different routes using an

play07:38

element in the ui

play07:40

thank you for watching make sure to

play07:42

subscribe to the channel and i'll see

play07:44

you in the next one

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
React RouterWeb NavigationRoute ConfigurationDynamic RoutingFrontend DevelopmentUI ComponentsWeb DesignJavaScript TutorialUser InterfaceWeb Application