Search Header Logo
Introduction to react-router-dom

Introduction to react-router-dom

Assessment

Presentation

Computers

University

Practice Problem

Medium

Created by

Zh L

Used 5+ times

FREE Resource

18 Slides • 10 Questions

1

media

Introduction to react-router-dom

2

What is react-router-dom?

You may be curious about the role of React Router.

How does it contribute to app development?

And what is the precise function of a router, after all?

3

Typical Website Routing (Server-Side Routing):

  • Involves requesting a new page from the server whenever a link is clicked.

  • Provides a new page to the user for each request, resulting in full page refresh every time which is not performance-efficient!

media

Source: ​https://www.telerik.com/blogs/what-is-react-router-what-is-react-location

4

React Website Routing
(Client-Side Routing):

  • Prevents server requests whenever a link is clicked hence no page refresh even when the URL changes.

  • Components are rendered based on the current URL without making server requests providing dynamic content without full page reloads.

Source: ​https://www.telerik.com/blogs/what-is-react-router-what-is-react-location

media

5

Single Page Application (SPA)

A Single Page Application (SPA) is a type of website where everything happens on a single page.

SPAs dynamically updates its content
by dynamically fetching new data from the web server/back-end in response to user interactions.

Instead of loading entirely new pages, SPAs
show or hide specific components when needed.

6

Anatomy of a Single Page Application (SPA)

media

7

react-router-dom

React Router is a popular library in the React ecosystem that provides a powerful and flexible way to handle routing and navigation in web applications. It allows you to build single-page applications (SPAs) easily!

media

8

react-router-dom concepts

There are three primary categories of components in React Router:

  • Routers, like <BrowserRouter> and <HashRouter>

  • Route Matchers, like <Route> and <Switch>

  • Navigation or Route Changers, like <Link>, <NavLink>, and <Redirect>

9

react-router-dom concepts

Routers: React Router component like BrowserRouter manages the routing process.

In this example:
Typically rendered at the root of your element hierarchy. You’ll wrap your top-level <App> element in a router, like the shown in the example (index.js).

media

10

react-router-dom concepts

Route Matchers-> Uses Declarative Configuration to configure routing, where you specify which components to render for specific URLs.

media

In this example:

- When the user visits the home page at http://localhost:3000/, the RecordList component will be displayed.

- When the user navigates to http://localhost:3000/create, the Create component will be shown instead!

11

react-router-dom concepts

Navigation or Route Changers -> provides components like Link and NavLink, making it easy to create navigation links that automatically handle route changes when users click on them.

In this example:

- When the user click on the Home Link, the Route Matchers will pick up the "/" url path and attempt to match it to the corresponding component depending on the <Routes> paths.

media

12

react-router-dom concepts

URL Params -> Params are placeholders in the URL that begin with a colon, like the `:id` param defined in the route in this example.

In this example:

- URL parameters are helpful for accessing dynamic parts of the URL, such as fetching a specific record based on an ID parsed from the URL.

media

13

Scenario: React Router example

In this example, we will explore how React Router is commonly used to implement routing and navigation.

14

Step 1: install and setup react-router

  1. Install bootstrap and react-router using the command `npm install bootstrap react-router-dom`

  2. In your index.js file, import BrowserRouter, then wrap your root App component with the BrowserRouter component

media

15

Step 2: Generate the 4 components

Generate the following components using the command:
`npx generate-react-cli component {ComponentName}`

media

16

Step 3: Setup Navigation in Navbar

  1. Import NavLink navigation/route changer from react-router-dom

  2. Create navbar using NavLink

media
media

17

Step 4: Setup route matchers in app.js

  1. Import Create and Home Components

  2. Import Route and Routes route matchers to configure the router (Matches the URL to Corresponding Component!)

media

18

Step 5: Test that the routing is working !

media
media

19

Multiple Choice

What does SPA stand for in the context of web development?

1

Server Page Application

2

Single Page Application

3

Simple Page Application

4

Static Page Application

20

Multiple Choice

Which React Router DOM component is used to define a route in your application?

1

<Route>

2

<Link>

3

<Router>

4

<Switch>

21

Multiple Choice

What is the main difference between Server-side and Client-side routing?

1

Server-side routing relies on the server to process requests and send back new pages, while client-side routing uses JavaScript to update content on the same page.

2

Server-side routing uses JavaScript for navigation, while client-side routing sends requests to the server for each page change.

3

Server-side routing is primarily used in Single Page Applications (SPAs), while client-side routing is suitable for traditional web applications.

4

Server-side routing offers a faster and smoother user experience compared to client-side routing.

22

Multiple Choice

What is the main advantage of client-side routing in a web application?

1

No page refresh during navigation

2

Improved security

3

Faster server response times

4

Better SEO optimization

23

Multiple Choice

What is the purpose of BrowserRouter in React Router?

1

It defines the routes for server-side routing in a React application.


2

It is used to create browser-specific components in React.

3

It configures the browser's default behavior for navigation in a React application.

4

It sets up a router and manages the routing process in a React application.

24

Multiple Choice

What is the purpose of Route Matchers in React Router?

1

They validate user input in forms

2

They match URLs to components

3

They define server-side routes

4

They filter incoming HTTP requests

25

Multiple Choice

How can you navigate to a specific route using React Router?

1

Manually update the URL in the browser's address bar

2

Use the <a> tag with the href attribute

3

Call the window.navigate() function

4

Use the <Link> component

26

Multiple Choice

Which component is used to define a route in React Router?

1

<Navigate>

2

<Route>

3

<Link>

4

<Router>

27

Multiple Choice

Question image

What is the primary purpose of URL params in React Router?

e.g., /item_:id

1

To store sensitive data in the URL

2

To create complex route patterns

3

To pass dynamic data through the URL

4

To specify route transitions

28

Multiple Choice

Question image

In React Router, how do you access and extract URL params from a route?

1

Use the useLocation hook

2

Use the useParams hook

3

Use the useHistory hook

4

Use the useEffect hook

media

Introduction to react-router-dom

Show answer

Auto Play

Slide 1 / 28

SLIDE