简体   繁体   English

JavaScript 原型,如何在链接时分配变量?

[英]JavaScript prototype, how can I assign a variable while chaining?

I'm studying react.我正在研究反应。 I have a trouble with using prototype.我在使用原型时遇到了麻烦。

I want to assign a variable while chaining.我想在链接时分配一个变量。 The number of array returned by slice function should be assigned on count state. slice function 返回的数组数量应分配在count state 上。

What I want to implement is not to go to the page which is over the count of contents.我要实现的不是 go 到超过内容计数的页面。 To implement that, I thought I should assign count state when slice function is working.为了实现这一点,我想我应该在slice function 工作时分配count state。 How can I assign count ?如何分配count

I typed codes in sandbox.我在沙箱中输入代码。 https://codesandbox.io/s/blissful-rosalind-15e7od?file=/src/App.js Below is the same contents. https://codesandbox.io/s/blissful-rosalind-15e7od?file=/src/App.js下面是相同的内容。

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

export default function App() {
  const rows = [
    { id: 1, name: "a" },
    { id: 2, name: "b" },
    { id: 3, name: "c" },
    { id: 4, name: "d" },
    { id: 5, name: "e" },
    { id: 6, name: "f" },
    { id: 7, name: "g" },
    { id: 8, name: "h" },
    { id: 9, name: "i" },
    { id: 10, name: "j" },
    { id: 11, name: "k" },
    { id: 12, name: "l" }
  ];

  const [count, setCount] = useState(rows.length);
  const [page, setPage] = useState(0);
  const [rowsPerPage, setRowsPerPage] = useState(3);

  const handleLeft = () => {
    page > 1 && setPage(page - 1);
  };

  const handleRight = () => {
    page < Math.ceil(count / rowsPerPage) && setPage(page + 1);
  };

  return (
    <>
      <h3>
        count: {count}, page: {page}, rowsPerPage: {rowsPerPage}
      </h3>
      <button onClick={handleLeft}>left</button>
      <button onClick={handleRight}>right</button>
      {rows
        .filter((item) => item.id > 3)
        .slice(
          ...(rowsPerPage > 0
            ? [page * rowsPerPage, page * rowsPerPage + rowsPerPage]
            : [])
        )
        .map((item) => (
          <div key={item.id}>{item.id}</div>
        ))}
    </>
  );
}

Please help me.请帮我。 Thanks in advance.提前致谢。

Why don't you try something like this sandbox link你为什么不试试这个沙盒链接

Is there any specific reason as to why you wish to proceed with updating the prototype for your use case?您希望继续更新用例的原型是否有任何具体原因?

[UPDATED SANDBOX LINK] [更新沙盒链接]

Please find your solution on below link.请在下面的链接中找到您的解决方案。 There's some problem with the pagination logic but I am sure you'll take care of it.分页逻辑存在一些问题,但我相信你会处理它。 Hope the below sandbox link helps希望下面的沙箱链接有帮助

Link 关联

Custom hooks implementation自定义钩子实现

const usePagination = (data = [], rowPerPage = 3) => {
  const [currentpage, setCurrentPage] = useState(0);
  const totalItems = data.length;
  const maxPages = Math.ceil(totalItems / rowPerPage);

  const next = () => {
    setCurrentPage((page) => {
      const _page = page + 1;
      return _page < maxPages ? _page : page;
    });
  };
  const prev = () => {
    setCurrentPage((page) => {
      const _page = page - 1;
      return _page >= 0 ? _page : page;
    });
  };
  const itemsToList = useMemo(() => {
    const start = currentpage * rowPerPage;
    const end = start + rowPerPage;
    return data.slice(start, end);
  }, [rowPerPage, currentpage, data]);

  return {
    prev,
    next,
    itemsToList,
    maxPages,
    totalItems,
    currentpage
  };
};

code sandbox link代码沙箱 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM