Set up your React project:
npm install react react-dom tailwindcss
Create a Pagination component:
src
directory, create a new file called Pagination.js
.Pagination.js
and import the necessary React components and hooks:import React from 'react';
Pagination
:function Pagination() {
return (
<div className="flex justify-center mt-4">
{/* Pagination component code goes here */}
</div>
);
}
export default Pagination;
Add pagination logic:
Pagination
component, define the necessary state variables and functions:function Pagination() {
const [currentPage, setCurrentPage] = React.useState(1);
const totalPages = 10; // Replace with the actual total number of pages
const goToPage = (pageNumber) => {
setCurrentPage(pageNumber);
};
// Add additional logic to fetch data and update the total number of pages
return (
<div className="flex justify-center mt-4">
{/* Pagination component code goes here */}
</div>
);
}
Render pagination buttons:
Pagination
component, render the pagination buttons based on the current page and total number of pages:function Pagination() {
// Existing code
return (
<div className="flex justify-center mt-4">
{Array.from({ length: totalPages }, (_, i) => (
<button
key={i}
className={`mx-1 px-3 py-1 rounded-md ${
currentPage === i + 1 ? 'bg-blue-500 text-white' : 'bg-gray-200'
}`}
onClick={() => goToPage(i + 1)}
>
{i + 1}
</button>
))}
</div>
);
}
Style the pagination component using Tailwind CSS:
src/index.css
) and import Tailwind CSS:@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
.pagination button {
transition: background-color 0.3s, color 0.3s;
}
.pagination button:hover {
background-color: #4299e1;
color: #ffffff;
}
pagination
class to the container <div>
in the Pagination
component:function Pagination() {
// Existing code
return (
<div className="flex justify-center mt-4 pagination">
{/* Pagination buttons */}
</div>
);
}
Use the Pagination component in your application:
Pagination
component:
```jsx
import Pagination from