我在列react 待办事项 list . 它的Find工作没有任何错误.

然而,当我进入列表时,它不会出现(它只在我点击页面时出现). 我试图更改handleSubmit函数,但仍然无法弄清楚.我怎么才能修好它呢?

这是我的代码.它具有分页功能,每5个列表,并移动到下一页.

import React, { useState, useEffect } from "react";
import "./App.css";

export default function App() {
  const [todoData, setTodoData] = useState([]);
  const [value, setValue] = useState("");

  const [currentPage, setCurrentPage] = useState(1);
  const [currentTodos, setCurrentTodos] = useState([]);
  const todosPerPage = 5;

  const btnStyle = {
    color: "#fff",
    border: "none",
    padding: "5px 9px",
    borderRadius: "50%",
    cursor: "pointer",
    float: "right",
  };

  const getStyle = (finished) => {
    return {
      padding: "10px",
      borderBottom: "1px #ccc dotted",
      textDecoration: finished ? "line-through" : "none",
    };
  };

  const handleClick = (id) => {
    let newTodoData = todoData.filter((data) => data.id !== id);
    setTodoData(newTodoData);
  };

  const handleChange = (e) => {
    setValue(e.target.value);
  };

  const handleSubmit = (e) => {
    e.preventDefault();

    let newTodo = {
      id: Date.now(),
      title: value,
      finished: false,
    };

    setTodoData((prev) => [...prev, newTodo]);

    if ((todoData.length + 1) % todosPerPage === 1) {
      setCurrentPage((prevPage) => prevPage + 1);
    }

    setValue("");
  };

  const handleFinished = (id) => {
    let newTodoData = todoData.map((data) => {
      if (data.id === id) {
        data.finished = !data.finished;
      }
      return data;
    });
    setTodoData(newTodoData);
  };

  useEffect(() => {
    const indexOfLastTodo = currentPage * todosPerPage;
    const indexOfFirstTodo = indexOfLastTodo - todosPerPage;
    const currentTodos = todoData
      .filter(
        (_, index) => index >= indexOfFirstTodo && index < indexOfLastTodo
      )
      .slice(0, todosPerPage);

    setCurrentTodos(currentTodos);
  }, [todoData, currentPage, todosPerPage]);

  return (
    <div className="container">
      <div className="todoBlock">
        <div className="title">
          <h1 className="title" style={{ textAlign: "center" }}>
            Todo List
          </h1>
        </div>
        {currentTodos.map((data) => (
          <div style={getStyle(data.finished)} key={data.id}>
            <input
              type="checkbox"
              onChange={() => handleFinished(data.id)}
              defaultChecked={false}
            />
            {data.title}
            <button style={btnStyle} onClick={() => handleClick(data.id)}>
              x
            </button>
          </div>
        ))}

        <form style={{ display: "flex" }} onSubmit={handleSubmit}>
          <input
            type="text"
            name="value"
            style={{ flex: "10", padding: "5px" }}
            placeholder="Here"
            value={value}
            onChange={handleChange}
          />

          <button className="btn" type="submit" style={{ flext: "1" }}>
            Enter
          </button>
        </form>

        <div className="pagination">
          {Array.from(
            { length: Math.ceil(todoData.length / 5) },
            (_, i) => i + 1
          ).map((pageNumber) => (
            <button
              key={pageNumber}
              onClick={() => setCurrentPage(pageNumber)}
              className={currentPage === pageNumber ? "active" : ""}
            >
              {pageNumber}
            </button>
          ))}
        </div>
      </div>
    </div>
  );
}

谢谢!

推荐答案

const [currentPage, setCurrentPage] = useState(0);

将页面的默认值设置为0

这个改装是可以的.

useEffect(() => {
    const indexOfLastTodo = currentPage * todosPerPage;
    const indexOfFirstTodo = indexOfLastTodo - todosPerPage;
    console.log('todoData:',todoData)
 console.log(indexOfLastTodo,indexOfFirstTodo)
    const currentTodos = todoData
      .slice(indexOfFirstTodo,indexOfLastTodo)
      console.log(currentTodos);
      setCurrentTodos(currentTodos);

  }, [todoData, currentPage]);

多个日志(log)将揭示问题

Reactjs相关问答推荐

如何实现黑暗模式 map ?

react 表复选框不对任何操作做出react

如何将图像(在代码中称为自己)定位在蓝色圈内的点上?我正在使用material UI和Reaction

如何从react 18-nextjs 14应用程序路由中的客户端组件呈现服务器组件?

页面永远加载API/从数据库获取数据

用于获取带有事件处理程序的API的动态路由

React-chartjs-2:红色和绿色之间的差距

我如何使用 React toastify (Promise) toast 和邮箱 js

Stripe Checkout:是否可以创建不自动续订的订阅?

React - 拖放 UML

从服务器获取数据时未定义,错误非法调用

为嵌套路由配置一个不存在的 url 的重定向

antd 找不到 comments

React Native - 身份验证 - 触发值以更改 Auth 和 UnAuth 堆栈导航器

将路径数组传递给Route会引发错误

React Router 6.4CreateBrowserRouter子元素不显示

使用 React、Redux 和 Firebase 的无限循环

React如何在用户登录后等待当前用户数据被获取

在 React 中使用使用状态挂钩合并两个数组

React useEffect hooks return () => cleanup() vs return cleanup