Routing
Routing
Routing In
React
@oluwakemi Oluwadahunsi
What is React Routing?
Routing in React is a way to navigate between
different pages or views in a React application.
@oluwakemi Oluwadahunsi
Why Use Client-Side
Routing?
Faster Navigation: Only the content that
changes is updated, leading to faster page
transitions.
@oluwakemi Oluwadahunsi
React Router Overview
React Router is the most popular library for
routing in React applications. It provides a simple
API to define routes and map them to React
components.
@oluwakemi Oluwadahunsi
How Does Routing Work in React?
1. React Router: To implement routing in React,
we use a popular library called React Router. This
library helps manage navigation and rendering of
components based on the URL.
@oluwakemi Oluwadahunsi
BrowserRouter (Router): Wraps the application
and provides routing capabilities.
<Link>: Creates navigation links to different
routes (Home, About, and Contact).
<Routes> and <Route>: Defines the different
paths (/, /about, /contact) and the components
(Home, About, Contact) to be rendered when
those paths are accessed.
@oluwakemi Oluwadahunsi
Dynamic Routing with URL
Parameters
React Router allows you to create dynamic routes
with parameters that change based on the URL.
This is useful for pages like user profiles, product
details, etc.
@oluwakemi Oluwadahunsi
Step 2: Create the UserProfile Component and add
these codes:
@oluwakemi Oluwadahunsi
Now, use the Link component to navigate to a
dynamic route.
@oluwakemi Oluwadahunsi
Nested Routes
Nested Routing in React is a technique that allows
you to define routes within other routes, creating
a hierarchy or "nesting" of routes. This is useful
when you have a component that has its own set of
sub-routes or pages.
@oluwakemi Oluwadahunsi
Programmatic Navigation
Sometimes, you need to navigate to a different
route programmatically, such as after a form
submission or a user action.
React Router provides the useNavigate hook to
perform navigation.
Instead of relying on traditional <Link>
components or anchor tags to navigate between
pages, useNavigate provides a way to perform
navigation through JavaScript code.
This is particularly useful in situations where you
need to change the route based on certain
conditions, such as after form submission, button
clicks, or user actions.
@oluwakemi Oluwadahunsi
The useNavigate hook is imported from the react-
router-dom library.
const navigate = useNavigate(); assigns the
navigate function to a variable. This function can
now be used to perform navigation actions.
@oluwakemi Oluwadahunsi
1. Define a component to handle protected routes:
@oluwakemi Oluwadahunsi
When a user tries to access the /dashboard route:
@oluwakemi Oluwadahunsi
Handling 404 Pages (Not Found)
This is a good practice of managing situations
when a user tries to access a route that doesn't
exist in your application. This is commonly referred
to as a "404 Page" or "Not Found" page.
@oluwakemi Oluwadahunsi
In your main App.jsx file (or wherever you have
defined your routes), import the NotFound
component and add a route that matches any
path that isn't explicitly defined.
@oluwakemi Oluwadahunsi
<Route path="*">: The path="*" route acts as a
catch-all. It matches any route that is not
explicitly defined above it. This is where the
NotFound component is rendered, ensuring
that any undefined path will show the 404
page.
@oluwakemi Oluwadahunsi
I hope you found this material
useful and helpful.
Remember to:
Like
&
Share with your network, be
helpful to someone 👌
@oluwakemi Oluwadahunsi
Hi There!
Thank you for reading through
Did you enjoy this knowledge?
kodemaven-portfolio.vercel.app