ReactJS - React Router, Route With Parameters, Child Routes 1
ReactJS - React Router, Route With Parameters, Child Routes 1
REACT JS
ROUTING IN REACT
Ground Rules
For a successful class, please:
• Arrive on time.
• Turn off cell phones.
• Assist your colleagues; show respect to all individuals regardless of their skill and
knowledge level.
• Do not use class time to surf the net, check e-mail, or use instant messaging.
• Adhere to attendance policy as directed by your local training coordinator.
• Make the most of face-to-face training; ask questions and share your own insights and
expertise.
• Leave the room only during break times or in the event of an emergency.
• Manage expectations for your other responsibilities.
2
Agenda
1 Understand what is router, why router is
required
3 Child Router
4 Demo
5 Activity
6 Passing Parameters
3
Introduction to Routing
• React Router is a standard library for routing in React.
4
Introduction to Routing
5
Introduction to Routing (Cont.…)
react-router- react-router-
react-router
dom native
This package
provides environment This package provides
This package provides specific environment specific
core functionalities to functionalities and functionalities and
achieve routing in components which components which
react are useful to run are useful to run
application on application for mobile
browser
3
6
Introduction to Routing (Cont.…)
7
Introduction to Routing (Cont.…)
There are some basic functionalities are
required to achieve routing in an application,
these will be achieved in a proper steps
Step 2
Step 3
Step 1 Identify how many
Corresponding to the
Identify which type of components are
routes which are
router is required present inside
provided in step2, the
depending on type of application, based on
view of an application
application. There are that we must create
must be adjusted. This
only 2 choices either route’s which will have
will allow a user to click
react-router-dom or path(url) and
on respective link to
react-router-native corresponding
display component
component to display
8
Introduction to Routing (Cont.…)
These steps can be achieved using different
routing functions from react-router-dom
library
Step 3
Step 1 Step 2
This can be achieved
This can be achieved This can be achieved
using
using using
<Link>
<Router> <Route>
9
Introduction to Routing (Cont…) - <Router>
• The common low-level interface for all router components.
• Typically apps will use one of the high-level routers instead
• Below are the different types of Router
• <BrowserRouter> - uses the HTML5 history API to keep your UI in sync with the URL.
• <HashRouter> - uses the hash of URL to memorize all things and support old browsers or the React application using
client-side Router
• <MemoryRouter> A <Router> that keeps the history of your “URL” in memory. Useful in tests and non-browser
environments like React Native
• <NativeRouter> A <Router> for iOS and Android apps built using React Native.
• <StaticRouter> useful in server-side rendering scenarios name: static. It’s also useful in simple tests when you just need
to plug in a location and make assertions on the render output.
10
Introduction to Routing (Cont…) - <BrowserRouter>
11
Introduction to Routing (Cont…) - <Route>
Using this one can provide all route
information. This will be imported using
Import {Route} from ‘react-router-dom’
12
Introduction to Routing (Cont…) - <Link>
This element is mainly used to provide user a
way to click on the link so that appropriate
component can displayed on the view
13
Demo -- Routing
Create new react project
create-react-app reactexample
14
Demo – Routing (Cont.…) - Explanation
Inside index.js
BrowserRouter is
used here as the
application is
created to execute
on the browser.
Also the alias name
is provided as
Router
15
Demo – Routing (Cont.…) - Explanation
Inside index.js
This is required to
provide route details
17
Demo – Routing (Cont.…) - Explanation
Create new component NavigationBar.js
This is required to
provide view section
to user, so that a user
can click on
whichever
component he/she
wants to navigate
Link element have
“to” which matches
the path provided in
Route element. Link
element also have
text to display on the
view which can be
seen by the user and
click on the link to
navigate
18
Demo – Routing (Cont.…)
Inside App.js
The NavigationBar
component is
rendered here
Demo – Routing (Cont.…)
Output
Demo – Routing (Cont.…)
Output when clicked
on Home Link
Demo – Routing (Cont.…)
Output when clicked
on Customer Link
Routing with child routes
In the previous module we have
discussed how to achieve routing in react
using react-router-dom library
25
Demo – Routing with child routes (Cont…) --
Explanation
26
Demo – Routing with child routes (Cont…) --
Explanation
Links are provided in App.js , which are seen by
the user. And user can click on any one of the
link
Output
Demo – Routing with child routes (Cont.…)
ReactDOM.render(
<Router>
<Route path="/" component={App}></Route>
<Route path="/product" component={Product}></Route>
<Route path="/about" component={About}></Route>
<Route path="/contact" component={Contact}></Route>
<Route path="/product/productdetails/:pname/:pid/:manf/:price"
component={ProductDetails}></Route>
</Router>, document.getElementById('root'));
<td><Link to={
`/product/productdetails/
${product.name}/ When the user clicks
${product.productno}/ on the link then
${product.manufacturer}/ corresponding
${product.price} parameter will be sent
`
}>{product.productno}</Link></td>
Passing Parameters Routing - match
A match object contains information about how a <Route path> matched the URL
• params - Key/value pairs parsed from the URL .
• isExact - For exact URL Matching returns true.
• path - Pattern used to match, Used in Nesting of URL as well.
• url - The matched portion of the URL.
Displays which
parameter is passed from
Digital component
Module Summary
Now, you should be able to:
• What is router
• Importance of router
• How to configure router
• Child Router
• Passing Parameter
41
Thank You
42