Advanced Routing
Installing react-router​
To get started with react-router
, you'll need to install it using npm:
npm install react-router
Setting Up Routing​
The react-router-dom library provides a BrowserRouter
, Routes
and Route
components that enables client-side routing. Here's how you can set it up:
1. Import Required Dependencies​
In your src/main.jsx file, import the necessary dependencies:
import ReactDOM from "react-dom/client";
import { BrowserRouter, Route, Routes } from "react-router";
import Home from "./views/Blogs/Blogs";
import Contact from "./views/Contact/Contact";
import About from "./views/About/About";
2. Setting-Up BrowserRouter component.​
BrowserRouter: The
BrowserRouter
component is used to wrap your application and enable client-side routing.Routes: The
Routes
component is used to define the routes for your application.Route: Each
<Route/>
defines a path (path attribute) and a corresponding component (element attribute) to render.
createRoot(document.getElementById("root")).render(
<BrowserRouter>
<Routes>
<Route path="/" element={<Blogs />} />
<Route path="/contact" element={<Contact />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
Hurray! Now your React application is ready to handle client-side routing.
Now Let's learn how advance routing works.​
Create a folder into src called
data
and create a file calledblogs-data.json
in it.Add the following data to the
blogs-data.json
file:
[
{
"id": 1,
"title": "Embrace the Present Moment",
"content": " Mindfulness helps you stay grounded in the present, reducing stress and increasing focus. A few minutes of being fully aware each day can transform your life.",
" author": "Abhimanyu ",
"date": "2023-08-01"
},
{
"id": 2,
"title": "Embrace the Present Moment",
"content": " Mindfulness helps you stay grounded in the present, reducing stress and increasing focus. A few minutes of being fully aware each day can transform your life.",
"author": "Abhimanyu ",
"date": "2023-02-01"
},
{
"id": 3,
"title": "The Power of Mindfulness",
"content": "Simple moments of mindfulness can help you reclaim control over stress. Start small, be present, and experience more peace in your day.",
"author": "Abhimanyu ",
"date": "2023-06-01"
},
{
"id": 4,
"title": "Slow Down to Find Peace",
"content": " Mindfulness helps you stay grounded in the present, reducing stress and increasing focus. A few minutes of being fully aware each day can transform your life.",
"author": "Abhimanyu ",
"date": "2022-04-01"
}
]
- Create a card component called
BlogCards
in thecomponents
folder.
function BlogCards({ id, title, content, author, date }) {
return (
<div>
<h1>{title}</h1>
<p>{content.length > 100 ? content.slice(0, 100) + "..." : content}</p>
<span>{author}</span>
<span>{date}</span>
</div>
);
}
export default BlogCards;
Provide css (styling)
according to your choice.
Output​
- Create a page called
Blogs
in theviews
folder and map theBlogCard
component to each blog post in theblogs-data.json
file.
import BlogCards from "../components/BlogCards";
import data from "../data/data.json";
function Blogs() {
return data.map((data, index) => {
const { id, title, content, author, date } = data;
return (
<BlogCards
key={index}
id={id}
title={title}
content={content}
author={author}
date={date}
/>
);
});
}
export default Blogs;

- Create a page called
Read
in theviews
folder, which displays the full content of a blog post.
function Read() {
return (
<div>
<h1></h1>
<p>content</p>
<span>author</span>
<span>date</span>
</div>
);
}
export default Read;
Provide css (styling)
according to your choice.
- Now define another route into main.jsx file for the
Read
page.
import Read from "./views/Read/Read";
// rest of your code from main.jsx
<Route path="/read/:id" element={<Read />} />;
- The colon
:
before id indicates that id is a dynamic parameter.
Don't forgot to import the Read
page in main.jsx
file.
- Now let's back to the BlogCards component and wrap the
BlogCards
component with theLink
component. Whenever theBlogCards
component is clicked, the user will be redirected to theRead
page.
import { Link } from "react-router";
function BlogCards({ id, title, content, author, date }) {
return (
<Link to={`/read/${id}`}>
<div className=" bg-gray-300 text-zinc-800 m-2 p-2 ">
<h1 className="font-bold text-2xl">{title}</h1>
<p>{content.length > 100 ? content.slice(0, 100) + "..." : content}</p>
<span>Published on: {date}</span>
<span className="text-sm mx-2">By: {author}</span>
</div>
</Link>
);
}
export default BlogCards;
- Now let's go back to the read page and make it dynamic.
import { useParams } from "react-router";
import { useState, useEffect } from "react";
import blogJsonData from "../data/data.json";
function Read() {
const { id } = useParams();
const [blogData, setBlogData] = useState({
id: null,
title: "",
content: "",
author: "",
date: "",
});
useEffect(() => {
const blog = blogJsonData.find((blog) => blog.id == id);
setBlogData(blog);
}, [id]);
return (
<div>
<h1>{blogData.title}</h1>
<p>{blogData.content}</p>
<span>{blogData.author}</span>
<span>{blogData.date}</span>
</div>
);
}
export default Read;
useParams: This hook (provided by React Router) is used to access the dynamic parameters from the URL.
const { id } = useParams() The component extracts the dynamic
id
from theURL
.
const blog = blogJsonData.find((blog) => blog.id == id);
setBlogData(blog);
}, [id]);
As above you can see Whenever dependencies
id
will be changed then theuseEffect
will be called. change the state ofblogData
with theid
of the blog post.find
method is used to find the blog post with the matchingid
.It is actually extracting the id from the url and comparing it with the id of the blog post and returning the blog post with the matching id.