| Component | Purpose |
|---|---|
| BrowserRouter | Wraps the entire application and enables client-side routing. |
| Routes | Acts as a container for all route definitions. |
| Route | Maps a URL path to a specific component. |
| Link | Used for navigation between routes without reloading the page. |
| NavLink | Similar to Link but provides styling for the active route. |
npm install react-router-domimport { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
import 'bootstrap/dist/css/bootstrap.min.css'
import { BrowserRouter } from 'react-router-dom'
createRoot(document.getElementById('root')).render(
<StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</StrictMode>,
)
import Header from "./components/Header";
import Footer from "./components/Footer";
import Home from "./components/Home";
import About from "./components/About";
import { Routes, Route } from "react-router-dom";
function App() {
return (
<>
<Header />
<div className="container-fluid mt-4">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</div>
<Footer />
</>
);
}
export default App;
import { Link, NavLink } from "react-router-dom";
function Header()
{
return(
<>
<nav className="navbar navbar-expand-lg navbar-dark bg-dark">
<div className="container">
<span className="navbar-brand">My React App</span>
<ul className="navbar-nav ms-auto">
<li className="nav-item">
<NavLink className="nav-link active" to="/" >Home</NavLink>
</li>
<li className="nav-item">
<NavLink className="nav-link" to="/about">About</NavLink>
</li>
</ul>
</div>
</nav>
</>
);
}
export default Header;
A top loading bar is a thin progress bar displayed at the top of the page. It visually indicates that some process is running in the background.
A top loading bar is commonly used when:
Using a loading bar improves user experience by giving feedback instead of leaving the user waiting.
npm install react-top-loading-bar
The following React hooks are used:
For documentattion Click here
In this example, the loading bar appears automatically whenever the user navigates from one page to another (for example, Home → About).
import { Routes, Route, useLocation } from "react-router-dom";
import { useEffect, useRef } from "react";
import LoadingBar from "react-top-loading-bar";
import Header from "./components/Header";
import Footer from "./components/Footer";
import Home from "./components/Home";
import About from "./components/About";
function App() {
const loadingRef = useRef(null);
const location = useLocation();
useEffect(() => {
loadingRef.current.continuousStart();
const timer = setTimeout(() => {
loadingRef.current.complete();
}, 800);
return () => clearTimeout(timer);
}, [location]);
return (
<>
<LoadingBar
color="#dc3545"
height={4}
shadow={true}
ref={loadingRef}
/>
<Header />
<div className="container-fluid mt-4">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</div>
<Footer />
</>
);
}
export default App;
Toastr is used to display non-blocking notification messages in a React application.
It replaces traditional alert() with clean, professional UI messages.
Toastr is commonly used to show:
Toastr improves user experience by providing instant feedback without interrupting the user.
npm install react-toastify
The ToastContainer must be added once at the root level of the application
(in App.jsx).
import { ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
Add ToastContainer inside the App.jsx:
<ToastContainer position="top-right" autoClose={3000} />
To show notifications, import the toast function in any component.
import { toast } from "react-toastify";
For documentattion Click here
In this practical, we add buttons in the About component to test different toastr messages.
import { toast } from "react-toastify";
function About() {
const showSuccess = () => {
toast.success("Operation completed successfully!");
};
const showError = () => {
toast.error("Something went wrong!");
};
const showInfo = () => {
toast.info("This is an information message.");
};
const showWarning = () => {
toast.warning("This is a warning message.");
};
return (
<div className="container mt-4">
<h2 className="text-success">About Page</h2>
<p>This page demonstrates toastr notifications in React.</p>
<button className="btn btn-success me-2" onClick={showSuccess}>
Success
</button>
<button className="btn btn-danger me-2" onClick={showError}>
Error
</button>
<button className="btn btn-info me-2" onClick={showInfo}>
Info
</button>
<button className="btn btn-warning" onClick={showWarning}>
Warning
</button>
</div>
);
}
export default About;
Infinite Scroll is a UI pattern where data loads gradually as the user scrolls
npm install react-infinite-scroll-componentimport { useState } from "react";
import InfiniteScroll from "react-infinite-scroll-component";
function Clients()
{
const allClients = Array.from({ length: 50 }, (_, i) => ({
id: i + 1,
name: `Client ${i + 1}`,
city: `City ${i + 1}`
}));
const [visibleClients, setVisibleClients] = useState(allClients.slice(0, 10));
const fetchMoreData = () => {
setTimeout(() => {
setVisibleClients(prev =>
allClients.slice(0, prev.length + 10)
);
}, 800);
};
return (
<div className="container mt-4">
<h2 className="text-primary mb-3">Clients List</h2>
<InfiniteScroll
dataLength={visibleClients.length}
next={fetchMoreData}
hasMore={visibleClients.length < allClients.length}
loader={<h6>Loading more clients...</h6>}
>
<table className="table table-bordered">
<thead className="table-dark">
<tr>
<th>ID</th>
<th>Name</th>
<th>City</th>
</tr>
</thead>
<tbody>
{visibleClients.map(client => (
<tr key={client.id}>
<td>{client.id}</td>
<td>{client.name}</td>
<td>{client.city}</td>
</tr>
))}
</tbody>
</table>
</InfiniteScroll>
</div>
);
}
export default Clients;
<Route path='/clients' element={<Clients></Clients>}></Route> <li className="nav-item">
<NavLink className="nav-link" to="/clients">Clients</NavLink>
</li>