当前位置:首页 > React

react实现前端分页

2026-01-27 07:39:23React

实现前端分页的基本思路

前端分页的核心在于将全部数据一次性加载到前端,通过JavaScript控制当前页的数据展示。React中可以通过状态管理当前页码和每页数据量,利用数组切片实现数据分页。

基本实现步骤

安装React项目基础依赖(如未创建)

npx create-react-app pagination-demo
cd pagination-demo

创建分页组件

import { useState } from 'react';

function Pagination({ data, itemsPerPage }) {
  const [currentPage, setCurrentPage] = useState(1);
  const totalPages = Math.ceil(data.length / itemsPerPage);

  const getCurrentData = () => {
    const startIndex = (currentPage - 1) * itemsPerPage;
    const endIndex = startIndex + itemsPerPage;
    return data.slice(startIndex, endIndex);
  };

  const handlePageChange = (page) => {
    setCurrentPage(page);
  };

  return (
    <div>
      <ul>
        {getCurrentData().map((item, index) => (
          <li key={index}>{item}</li>
        ))}
      </ul>

      <div>
        {Array.from({ length: totalPages }, (_, i) => i + 1).map((page) => (
          <button
            key={page}
            onClick={() => handlePageChange(page)}
            disabled={page === currentPage}
          >
            {page}
          </button>
        ))}
      </div>
    </div>
  );
}

使用分页组件

react实现前端分页

function App() {
  const mockData = Array.from({ length: 100 }, (_, i) => `Item ${i + 1}`);

  return (
    <div>
      <h1>前端分页示例</h1>
      <Pagination data={mockData} itemsPerPage={10} />
    </div>
  );
}

优化分页组件

添加分页导航增强功能

const PaginationControls = ({ currentPage, totalPages, onPageChange }) => (
  <div>
    <button 
      onClick={() => onPageChange(1)} 
      disabled={currentPage === 1}
    >
      首页
    </button>

    <button 
      onClick={() => onPageChange(currentPage - 1)} 
      disabled={currentPage === 1}
    >
      上一页
    </button>

    {Array.from({ length: totalPages }, (_, i) => i + 1).map(page => (
      <button
        key={page}
        onClick={() => onPageChange(page)}
        disabled={page === currentPage}
      >
        {page}
      </button>
    ))}

    <button 
      onClick={() => onPageChange(currentPage + 1)} 
      disabled={currentPage === totalPages}
    >
      下一页
    </button>

    <button 
      onClick={() => onPageChange(totalPages)} 
      disabled={currentPage === totalPages}
    >
      末页
    </button>
  </div>
);

性能优化建议

对于大数据量情况,考虑以下优化方案:

react实现前端分页

虚拟滚动技术替代分页

import { FixedSizeList } from 'react-window';

function VirtualList({ data }) {
  return (
    <FixedSizeList
      height={400}
      width={300}
      itemSize={50}
      itemCount={data.length}
    >
      {({ index, style }) => (
        <div style={style}>
          {data[index]}
        </div>
      )}
    </FixedSizeList>
  );
}

分页器样式优化方案

.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
}

.pagination button[disabled] {
  color: #999;
  cursor: not-allowed;
}

.pagination button.active {
  background: #007bff;
  color: white;
}

完整示例整合

将上述组件组合成完整解决方案:

import { useState } from 'react';
import './Pagination.css';

function Pagination({ data, itemsPerPage = 10 }) {
  const [currentPage, setCurrentPage] = useState(1);
  const totalPages = Math.ceil(data.length / itemsPerPage);

  const getCurrentData = () => {
    const startIndex = (currentPage - 1) * itemsPerPage;
    const endIndex = startIndex + itemsPerPage;
    return data.slice(startIndex, endIndex);
  };

  return (
    <div className="pagination-container">
      <ul className="data-list">
        {getCurrentData().map((item, index) => (
          <li key={index} className="data-item">{item}</li>
        ))}
      </ul>

      <PaginationControls 
        currentPage={currentPage}
        totalPages={totalPages}
        onPageChange={setCurrentPage}
      />
    </div>
  );
}

标签: 分页react
分享给朋友:

相关文章

如何生成react代码

如何生成react代码

使用 Create React App 生成项目 安装 Node.js 后,通过命令行工具运行以下命令创建新项目: npx create-react-app my-app cd my-app npm…

如何react页面

如何react页面

创建 React 页面 使用 create-react-app 快速初始化项目: npx create-react-app my-app cd my-app npm start 基础页面结构 在 s…

react如何更新

react如何更新

更新 React 版本的方法 检查当前 React 版本 运行以下命令查看项目中安装的 React 版本: npm list react 更新 React 及相关依赖 通过 npm 或 yarn…

react如何迭代

react如何迭代

在 React 中迭代数组或对象 React 提供了多种方式来处理数组或对象的迭代,以下是常见的方法: 使用 map 方法迭代数组 map 是 JavaScript 数组的内置方法,常用于在 Rea…

如何配置react

如何配置react

配置React项目的步骤 安装Node.js和npm 确保系统已安装Node.js(包含npm)。可通过官网下载并安装最新版本。安装完成后,运行以下命令验证版本: node -v npm -v 创建…

如何用react

如何用react

使用React的基本步骤 安装Node.js和npm 确保系统已安装Node.js和npm。Node.js自带npm,可从官网下载安装包。安装完成后,通过命令行验证版本: node -v npm -…