当前位置:首页 > 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
分享给朋友:

相关文章

vue搜索过后实现分页

vue搜索过后实现分页

实现 Vue 搜索后分页的方法 数据绑定与搜索逻辑 在 Vue 组件中定义数据属性,包括原始数据列表、搜索关键词、分页相关参数: data() { return { originalL…

vue前端分页怎么实现

vue前端分页怎么实现

使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 <el-pagination>,可以快速实现分页功能。需要安装 Element UI 并注册组件。…

react中monent如何获取日期

react中monent如何获取日期

使用 Moment.js 获取当前日期 在 React 中通过 Moment.js 获取当前日期,可以直接调用 moment() 函数。它会返回包含当前日期和时间的 Moment 对象。 impor…

react 如何引入jquery

react 如何引入jquery

引入 jQuery 到 React 项目 在 React 项目中引入 jQuery 可以通过多种方式实现,但需要注意 React 和 jQuery 操作 DOM 的方式可能冲突,因此建议仅在必要时使用…

vue分页实现

vue分页实现

Vue 分页实现方法 基础分页组件实现 创建一个简单的分页组件,包含上一页、下一页和页码按钮。组件接收当前页码、总页数等参数,通过事件通知父组件页码变化。 <template> &l…

react如何开发组件

react如何开发组件

React 组件开发基础 React 组件分为函数组件和类组件两种形式。函数组件是现代 React 开发的主流方式,结合 Hooks 可以实现完整功能。 函数组件示例: function Gr…