
Introduction to react-router-dom
Presentation
•
Computers
•
University
•
Practice Problem
•
Medium
Zh L
Used 5+ times
FREE Resource
18 Slides • 10 Questions
1
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!
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
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)
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!
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).
10
react-router-dom concepts
Route Matchers-> Uses Declarative Configuration to configure routing, where you specify which components to render for specific URLs.
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.
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.
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
Install bootstrap and react-router using the command `npm install bootstrap react-router-dom`
In your index.js file, import BrowserRouter, then wrap your root App component with the BrowserRouter component
15
Step 2: Generate the 4 components
Generate the following components using the command:
`npx generate-react-cli component {ComponentName}`
16
Step 3: Setup Navigation in Navbar
Import NavLink navigation/route changer from react-router-dom
Create navbar using NavLink
17
Step 4: Setup route matchers in app.js
Import Create and Home Components
Import Route and Routes route matchers to configure the router (Matches the URL to Corresponding Component!)
18
Step 5: Test that the routing is working !
19
Multiple Choice
What does SPA stand for in the context of web development?
Server Page Application
Single Page Application
Simple Page Application
Static Page Application
20
Multiple Choice
Which React Router DOM component is used to define a route in your application?
<Route>
<Link>
<Router>
<Switch>
21
Multiple Choice
What is the main difference between Server-side and Client-side routing?
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.
Server-side routing uses JavaScript for navigation, while client-side routing sends requests to the server for each page change.
Server-side routing is primarily used in Single Page Applications (SPAs), while client-side routing is suitable for traditional web applications.
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?
No page refresh during navigation
Improved security
Faster server response times
Better SEO optimization
23
Multiple Choice
What is the purpose of BrowserRouter in React Router?
It defines the routes for server-side routing in a React application.
It is used to create browser-specific components in React.
It configures the browser's default behavior for navigation in a React application.
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?
They validate user input in forms
They match URLs to components
They define server-side routes
They filter incoming HTTP requests
25
Multiple Choice
How can you navigate to a specific route using React Router?
Manually update the URL in the browser's address bar
Use the <a> tag with the href attribute
Call the window.navigate() function
Use the <Link> component
26
Multiple Choice
Which component is used to define a route in React Router?
<Navigate>
<Route>
<Link>
<Router>
27
Multiple Choice
What is the primary purpose of URL params in React Router?
e.g., /item_:id
To store sensitive data in the URL
To create complex route patterns
To pass dynamic data through the URL
To specify route transitions
28
Multiple Choice
In React Router, how do you access and extract URL params from a route?
Use the useLocation hook
Use the useParams hook
Use the useHistory hook
Use the useEffect hook
Introduction to react-router-dom
Show answer
Auto Play
Slide 1 / 28
SLIDE
Similar Resources on Wayground
21 questions
SOAL LATIHAN 1 KELAS XII TKJ
Presentation
•
University
21 questions
custom Pc
Presentation
•
University
22 questions
Angular
Presentation
•
University
24 questions
ITCC101-L2(203A)
Presentation
•
University
22 questions
ITF+ Most Missed from 3/22
Presentation
•
12th Grade
18 questions
เรื่องุอุปกรณ์ในระบบเครือข่าย
Presentation
•
KG - University
21 questions
2do EXAMEN PARCIAL TDDE a 2021
Presentation
•
University
Popular Resources on Wayground
20 questions
STAAR Review Quiz #3
Quiz
•
8th Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
6 questions
Marshmallow Farm Quiz
Quiz
•
2nd - 5th Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
20 questions
Context Clues
Quiz
•
6th Grade
20 questions
Inferences
Quiz
•
4th Grade
19 questions
Classifying Quadrilaterals
Quiz
•
3rd Grade
12 questions
What makes Nebraska's government unique?
Quiz
•
4th - 5th Grade