You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React分页功能异常排查:使用react-paginate组件时所有笔记条目全部显示问题

问题定位与解决方案

你的分页组件计算页码逻辑是正确的,但所有笔记仍然全部展示的核心原因是:NoteList组件始终在渲染全部笔记数据,并没有使用分页组件计算出的当前页数据。

分页组件Pagination内部确实正确计算了当前页该展示的条目,但这些数据只存在于组件内部,没有传递给NoteList用于渲染。我们需要把分页的状态管理提升到App组件(因为App是数据源的持有者),让App直接控制要展示的当前页数据,再传递给NoteList。

具体修改步骤:

1. 重构App.js,接管分页逻辑

把原本在Pagination里的分页状态和计算逻辑移到App组件,同时结合搜索功能,确保分页基于搜索后的结果:

import { useState, useEffect } from "react";
import { nanoid } from 'nanoid';
import NoteList from "./components/NoteList";
import './App.css';
import Search from "./components/Search";
import Header from "./components/Header";
import Pagination from "./components/Pagination";

const App = () => {
  const [notes, setNotes] = useState([]);
  const [searchText, setSearchText] = useState('');
  const [darkMode, setDarkMode] = useState(false);
  const [showNote, setShowNote] = useState(true);
  // 添加分页核心状态
  const [itemOffset, setItemOffset] = useState(0);
  const itemsPerPage = 6; // 每页固定显示6条

  // 从localStorage加载历史笔记
  useEffect(() => {
    const saveNotes = JSON.parse(localStorage.getItem('notes-data'));
    if (saveNotes){
      setNotes(saveNotes);
    }
  }, []);

  // 实时保存笔记到localStorage
  useEffect(() => {
    localStorage.setItem('notes-data', JSON.stringify(notes))
  },[notes])

  // 先处理搜索过滤逻辑
  const filteredNotes = notes.filter(note => 
    note.title.toLowerCase().includes(searchText.toLowerCase())
  );

  // 计算当前页要展示的笔记和总页数
  const endOffset = itemOffset + itemsPerPage;
  const currentNotes = filteredNotes.slice(itemOffset, endOffset);
  const pageCount = Math.ceil(filteredNotes.length / itemsPerPage);

  // 分页切换事件处理
  const handlePageClick = (event) => {
    const newOffset = (event.selected * itemsPerPage) % filteredNotes.length;
    setItemOffset(newOffset);
  };

  // 新增笔记逻辑
  const addNote = (inputText, text) => {
    const date = new Date();
    const newNote = {
      id: nanoid(),
      title: inputText,
      text: text,
      date: date.toLocaleString()
    }
    const newNotes = [newNote, ...notes];
    setNotes(newNotes)
  }

  // 删除笔记逻辑
  const deleteNote = (id) => {
    var response = window.confirm("Are you sure that you want to remove the note?");
    if (response){
      const notesUpdated = notes.filter((note) => note.id !== id)
      setNotes(notesUpdated);
    }
  }

  return (
    <div className={darkMode ? 'dark-mode' : ''}>
      <div className="container">
        <Header handleToggleTheme={setDarkMode} />
        <Search handleSearchNote={setSearchText} setShowNote={setShowNote} />
        {/* 现在传递的是当前页的笔记数据 */}
        <NoteList
          notes={currentNotes}
          handleAddNote={addNote}
          handleDeleteNote={deleteNote}
        />
        {/* 把总页数和分页点击事件传给Pagination组件 */}
        <Pagination 
          pageCount={pageCount}
          handlePageClick={handlePageClick}
        />
      </div>
    </div>
  )
}

export default App;

2. 简化Pagination.js,专注分页控件渲染

现在Pagination只需要负责渲染分页UI,不需要处理数据逻辑,接收pageCount和handlePageClick两个props即可:

import React from 'react'
import ReactPaginate from 'react-paginate';
import '../styles/Pagination.css';

const Pagination = ({ pageCount, handlePageClick }) => {
  return (
    <>
      <ReactPaginate
        breakLabel="..."
        nextLabel="next >"
        onPageChange={handlePageClick}
        pageRangeDisplayed={3}
        pageCount={pageCount}
        previousLabel="< previous"
        renderOnZeroPageCount={null}
        containerClassName="pagination"
        pageLinkClassName="page-num"
        previousLinkClassName="page-num"
        nextLinkClassName="page-num"
        activeLinkClassName="activee boxx"
      />
    </>
  );
}

export default Pagination;

修改逻辑说明

  • 状态提升:把分页的核心状态(偏移量、每页条数)放到App组件,因为App拥有原始笔记数据,更适合控制数据的展示范围。
  • 搜索与分页结合:先对笔记进行搜索过滤,再对过滤后的结果分页,符合用户使用逻辑(用户搜索后,分页基于搜索结果)。
  • 职责单一:Pagination只负责触发分页切换事件,App负责数据计算,NoteList负责渲染当前页数据,数据流转清晰。

这样修改后,你的笔记就会按照6-6-1的规则分页展示了,测试13条笔记时,第2页显示第7-12条,第3页显示第13条,完全符合预期。

内容的提问来源于stack exchange,提问作者Juan Jesús

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 19:37:27