Web Age Technology Webinar Series: Routing and Navigation With React-Router
Web Age Technology Webinar Series: Routing and Navigation With React-Router
Webinar Series
TOPICS
Routing and Navigation
Setting up the Dev Environment for react-router
Router
Routes
Route and Query Parameters
Routing and Redux
DEMONSTRATION
Review demo app code
Webinar Series
Chapter 1 - React Router
Objectives
Key objectives of this chapter
Routing and Navigation
Setting up the Dev Environment for react-router
Router
Routes
Route and Query Parameters
Routing and Redux
Chapter 1 - React Router
2
Chapter 1 - React Router
1.2 react-router
The react-router and related packages allow developers to implement routing
and navigation between React components.
Several packages are available:
◊ react-router - core package
◊ react-router-dom - for use with web applications
◊ react-router-native - for use with react-native applications
◊ react-router-redux - for web apps that also use redux
◊ react-router-config - for supports static routes for server-side rendering
We will be looking at react-router-dom in this chapter
3
Chapter 1 - React Router
Install react-router-dom
npm install react-router-dom --save
Import to the file that defines the component that implements routing:
import { BrowserRouter, Route, Link }
from 'react-router-dom'
4
Chapter 1 - React Router
5
Chapter 1 - React Router
6
Chapter 1 - React Router
Notes:
The complete component listing:
import React from 'react'
import { BrowserRouter, Route, Link } from 'react-router-dom'
...
7
Chapter 1 - React Router
8
Chapter 1 - React Router
This allows you to use the shorter term 'Router' in your JSX code
<Router>
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
</ul>
<hr/>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
</div>
</Router>
9
Chapter 1 - React Router
10
Chapter 1 - React Router
1.8 <Switch>
All Route components with matching paths are displayed. This allows for more
than one to be shown at the same time:
If you need to have only one Route in a set of Routes to be displayed you need
to wrap the Routes with the <Switch> component
<Router>
...
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
<Route path="*" component={Default} />
</Switch>
</Router>
If a navigation path doesn't match an existing Route then:
◊ The previously matched Route component is removed
◊ No new component is displayed
You can use the "*" path along with <Switch> to create a default route that gets
displayed when no other Route matches
11
Chapter 1 - React Router
Here we are redirecting from the exact root path "/" to the "/home" path
12
Chapter 1 - React Router
13
Chapter 1 - React Router
14
Chapter 1 - React Router
":index" which appears in the above Route will hold whatever value is placed
after '/details/' when navigating:
For example for this link:
<Link to='/details/P001'>Details</Link>
15
Chapter 1 - React Router
16
Chapter 1 - React Router
This link will match the following Route, even with 'exact' specified:
<Route exact path='/about' component={About} />
To get individual parameters you can do this (or use a 3 rd party lib):
const search = props.location.search;
const params = new URLSearchParams(search);
const name = params.get('name');
const email = params.get('email');
17
Chapter 1 - React Router
18
Chapter 1 - React Router
If you need to be able to rewind actions using Time Travel you will
More sophisticated users may wish to add the react-router-redux package. This
package incorporates the app's location url into the redux state thus allowing
you to rewind navigational actions use Time Travel.
19
Chapter 1 - React Router
1.16 Summary
In this Chapter we Covered:
Routing and Navigation
Setting up the Dev Environment for react-router
Router
Routes
Route and Query Parameters
Routing and Redux
20
UPCOMING CLASSES
Webinar Series
QUESTIONS?
?
Webinar Series
CONTACT
Webinar Series