当前位置:首页 > React

react分页功能实现

2026-01-27 08:28:41React

实现React分页功能的方法

分页组件的基本结构

创建一个独立的Pagination组件,接收当前页码、总页数和回调函数作为props。组件内部渲染页码按钮,并处理点击事件。

function Pagination({ currentPage, totalPages, onPageChange }) {
  return (
    <div className="pagination">
      <button 
        disabled={currentPage === 1}
        onClick={() => onPageChange(currentPage - 1)}
      >
        上一页
      </button>

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

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

数据分页逻辑

在父组件中管理数据的分页逻辑,通常使用useState来跟踪当前页码。

react分页功能实现

function DataList({ data }) {
  const [currentPage, setCurrentPage] = useState(1);
  const itemsPerPage = 10;

  const totalPages = Math.ceil(data.length / itemsPerPage);
  const paginatedData = data.slice(
    (currentPage - 1) * itemsPerPage,
    currentPage * itemsPerPage
  );

  return (
    <div>
      {/* 渲染分页数据 */}
      {paginatedData.map(item => (
        <div key={item.id}>{item.name}</div>
      ))}

      {/* 分页组件 */}
      <Pagination
        currentPage={currentPage}
        totalPages={totalPages}
        onPageChange={setCurrentPage}
      />
    </div>
  );
}

优化分页显示

对于大量页码的情况,可以限制显示的页码数量,避免UI混乱。

react分页功能实现

function getVisiblePages(currentPage, totalPages, maxVisible = 5) {
  let start = Math.max(1, currentPage - Math.floor(maxVisible / 2));
  let end = Math.min(totalPages, start + maxVisible - 1);

  start = Math.max(1, end - maxVisible + 1);

  return Array.from({ length: end - start + 1 }, (_, i) => start + i);
}

// 在Pagination组件中使用
{getVisiblePages(currentPage, totalPages).map(page => (
  <button
    key={page}
    onClick={() => onPageChange(page)}
    className={currentPage === page ? 'active' : ''}
  >
    {page}
  </button>
))}

与API集成

当数据来自API时,可以在分页变化时触发新的数据请求。

function ApiDataList() {
  const [data, setData] = useState([]);
  const [currentPage, setCurrentPage] = useState(1);
  const [totalPages, setTotalPages] = useState(1);

  useEffect(() => {
    fetch(`/api/data?page=${currentPage}`)
      .then(res => res.json())
      .then(({ items, total }) => {
        setData(items);
        setTotalPages(Math.ceil(total / 10));
      });
  }, [currentPage]);

  return (
    <div>
      {data.map(item => (
        <div key={item.id}>{item.name}</div>
      ))}

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

样式处理

添加基本CSS样式增强分页组件的视觉效果。

.pagination {
  display: flex;
  gap: 5px;
  margin-top: 20px;
}

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

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

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

标签: 分页功能
分享给朋友:

相关文章

h5能实现的功能

h5能实现的功能

H5能实现的功能 H5(HTML5)是一种现代的网页技术标准,具备丰富的功能和特性,适用于多种场景。以下是H5能够实现的主要功能: 多媒体支持 H5原生支持音频和视频播放,无需依赖Flash等插件。…

php分页代码简单实现

php分页代码简单实现

基本分页实现 在PHP中实现分页功能通常需要结合MySQL的LIMIT子句。以下是一个基础实现示例: <?php // 数据库连接 $conn = mysqli_connect("localh…

php实现的分页

php实现的分页

PHP 实现分页的方法 基本分页逻辑 分页的核心在于计算总页数、当前页码以及数据偏移量。假设每页显示 10 条数据: $current_page = isset($_GET['page']) ? (…

vue实现后台分页

vue实现后台分页

Vue 实现后台分页的方法 分页组件与数据绑定 在 Vue 中实现后台分页通常需要结合分页组件和后端 API。使用 el-pagination(Element UI)或自定义分页组件,通过 v-mod…

vue实现复选功能

vue实现复选功能

Vue 实现复选功能 基础实现 使用 v-model 绑定到数组实现多选功能。当复选框被选中时,其 value 会被添加到数组中;取消选中时,会从数组中移除。 <template>…

vue 实现多选功能

vue 实现多选功能

Vue 实现多选功能的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选功能。适用于复选框组(checkbox)或下拉多选(select multi…