当我尝试在其他分页选项卡之间切换时,我在反应分页中遇到了一个小错误,每次切换时网格内容都会增加,我该如何修复这个错误,你知道吗
我已经安装了这个 npm 包
https://www.npmjs.com/package/react-paginate
我将在下面提供我部署的项目:
https://shoe-ecommerce-ez1c.vercel.app/
import React, { useState, useContext } from "react"; import ReactPaginate from "react-paginate"; import { ShopContext } from "./Context/ShopContext"; import Products from "./Components/Products"; import { Link } from "react-router-dom"; function PaginationProdGrid() { const { products } = useContext(ShopContext); const [currentPage, setCurrentPage] = useState(0); const itemsPerPage = 6; const displayedItems = products.slice( currentPage * itemsPerPage, (currentPage + 1) * itemsPerPage ); const handlePageChange = (data) => { setCurrentPage(data.selected); }; return ( <> <div className="grid md:grid-cols-3 grid-cols-1 gap-4 m-8"> {displayedItems && displayedItems.length > 0 ? ( displayedItems.map((item) => ( <Link key={item._id} to={`/productInfo/${item._id}`}> <Products name={item.name} image={item.image} price={item.price} /> </Link> )) ) : ( <p className="text-center">No products available</p> )} </div> <div className="flex justify-center"> <ReactPaginate previousLabel={"previous"} nextLabel={"next"} breakLabel={"..."} breakClassName={"break-me"} pageCount={Math.ceil(products.length / itemsPerPage)} marginPagesDisplayed={2} pageRangeDisplayed={5} onPageChange={handlePageChange} containerClassName={ "pagination flex items-center text-black dark:text-white" } pageClassName={ "px-3 py-2 rounded-md mx-1 " } previousClassName={ "px-3 py-2 rounded-md mx-1 " } nextClassName={ "px-3 py-2 rounded-md mx-1 " } activeClassName={"bg-blue-500 "} /> </div> </> ); } export default PaginationProdGrid;
以上就是反应分页上的错误的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号