javascript - Pagination in ReactTypescript - Stack Overflow

So I am trying to achieve pagination functionality. Here I have crypto API and a table where data lies,

So I am trying to achieve pagination functionality. Here I have crypto API and a table where data lies, the thing I want to achieve is displaying 10 items per page. The real problem is that it is showing no errors, but it is not working. When I click on the next page button I am changing the pageNumber but it is not changing the data. I can't figure where the problem lies.

Coin Component

import React,  {lazy, Suspense, useEffect, useState} from 'react'
import {Coin} from '../../interface'
import './Coins.css'
import Pagination from "./Pagination";
const CoinTable = lazy(() => import('../../ponents/CoinData/CoinTable'))



 export const Coins:React.FC = () => {
  const [coinsData, setCoinsData] = useState<Coin[]>([])
  const [page, setPage] = useState(1);
  const [totalPages, setTotalPages] = useState(10);


  const handlePrevPage = (prevPage: number) => {
    setPage((prevPage) => prevPage - 1);
  };

  const handleNextPage = (nextPage: number) => {
    setPage((nextPage) => nextPage + 1);
  };
   
  

    
    const fetchData= async()=>{
      const response= await fetch(`? 
      vs_currency=usd&order=market_cap_desc&?${page}&per_page=10&sparkline=false`)
      const result = await response.json()
          setCoinsData(result); 
          setTotalPages(totalPages);
      }
      
      useEffect(()=>{
          fetchData()
      })
    return (
        <>
          <Suspense fallback={<div>Loading...</div>}>
          <table className="table" width="80%">  
        <thead>
          <tr>
            <th>Cryptocurrencies</th>
            <th>Price</th>
            <th>24H Change</th>
            <th>Market Cap</th>
          </tr>
        </thead>
        </table>
          {coinsData.map((coin)=>
         <CoinTable key={coin.id}
               {...coin}
                />
          )}
           <Pagination
            totalPages={totalPages}
            currentPage={page}
            handlePrevPage={handlePrevPage}
            handleNextPage={handleNextPage}
          />
          </Suspense>   
         </>
    )
}

Pagination


import React, {  } from "react";
import PropTypes from "prop-types";

 interface Props {
    currentPage: number;
    totalPages: number;
    handleNextPage: (page: number) => void;
    handlePrevPage: (page: number) => void;
  }
const Pagination:React.FC <Props>= ({
  currentPage,
  totalPages,
  handlePrevPage,
  handleNextPage,
}) => {
  return (
    <div className="pagination-button-wrapper">
      <button
        className="pagination-button"
        onClick={() => handlePrevPage(currentPage)}
        disabled={currentPage === 1}
      >
        &larr;
      </button>

      <span className="pagination-page-info">
        Page {currentPage} of {totalPages}
      </span>

      <button
        className="pagination-button"
        onClick={() => handleNextPage(currentPage)}
        disabled={currentPage === totalPages}
      >
       
      </button>
    </div>
  );
};

Pagination.propTypes = {
    currentPage: PropTypes.number.isRequired,
    totalPages: PropTypes.number.isRequired,
    handlePrevPage: PropTypes.func.isRequired,
    handleNextPage: PropTypes.func.isRequired,
  };

export default Pagination

So I am trying to achieve pagination functionality. Here I have crypto API and a table where data lies, the thing I want to achieve is displaying 10 items per page. The real problem is that it is showing no errors, but it is not working. When I click on the next page button I am changing the pageNumber but it is not changing the data. I can't figure where the problem lies.

Coin Component

import React,  {lazy, Suspense, useEffect, useState} from 'react'
import {Coin} from '../../interface'
import './Coins.css'
import Pagination from "./Pagination";
const CoinTable = lazy(() => import('../../ponents/CoinData/CoinTable'))



 export const Coins:React.FC = () => {
  const [coinsData, setCoinsData] = useState<Coin[]>([])
  const [page, setPage] = useState(1);
  const [totalPages, setTotalPages] = useState(10);


  const handlePrevPage = (prevPage: number) => {
    setPage((prevPage) => prevPage - 1);
  };

  const handleNextPage = (nextPage: number) => {
    setPage((nextPage) => nextPage + 1);
  };
   
  

    
    const fetchData= async()=>{
      const response= await fetch(`https://api.coingecko./api/v3/coins/markets? 
      vs_currency=usd&order=market_cap_desc&?${page}&per_page=10&sparkline=false`)
      const result = await response.json()
          setCoinsData(result); 
          setTotalPages(totalPages);
      }
      
      useEffect(()=>{
          fetchData()
      })
    return (
        <>
          <Suspense fallback={<div>Loading...</div>}>
          <table className="table" width="80%">  
        <thead>
          <tr>
            <th>Cryptocurrencies</th>
            <th>Price</th>
            <th>24H Change</th>
            <th>Market Cap</th>
          </tr>
        </thead>
        </table>
          {coinsData.map((coin)=>
         <CoinTable key={coin.id}
               {...coin}
                />
          )}
           <Pagination
            totalPages={totalPages}
            currentPage={page}
            handlePrevPage={handlePrevPage}
            handleNextPage={handleNextPage}
          />
          </Suspense>   
         </>
    )
}

Pagination


import React, {  } from "react";
import PropTypes from "prop-types";

 interface Props {
    currentPage: number;
    totalPages: number;
    handleNextPage: (page: number) => void;
    handlePrevPage: (page: number) => void;
  }
const Pagination:React.FC <Props>= ({
  currentPage,
  totalPages,
  handlePrevPage,
  handleNextPage,
}) => {
  return (
    <div className="pagination-button-wrapper">
      <button
        className="pagination-button"
        onClick={() => handlePrevPage(currentPage)}
        disabled={currentPage === 1}
      >
        &larr;
      </button>

      <span className="pagination-page-info">
        Page {currentPage} of {totalPages}
      </span>

      <button
        className="pagination-button"
        onClick={() => handleNextPage(currentPage)}
        disabled={currentPage === totalPages}
      >
       
      </button>
    </div>
  );
};

Pagination.propTypes = {
    currentPage: PropTypes.number.isRequired,
    totalPages: PropTypes.number.isRequired,
    handlePrevPage: PropTypes.func.isRequired,
    handleNextPage: PropTypes.func.isRequired,
  };

export default Pagination

Share Improve this question asked May 9, 2021 at 15:13 mura1mura1 4821 gold badge6 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Your useEffect is not re-running when page changes. Add it to the dependency array so that new data is fetched when it changes:

useEffect(()=>{
  fetchData()
}, [page])

Edit: based on the codesandbox, the URL should be:

https://api.coingecko./api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&page=${page}&per_page=10&sparkline=false

Instead of c&?${page}&.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745344079a4623463.html

相关推荐

  • javascript - Pagination in ReactTypescript - Stack Overflow

    So I am trying to achieve pagination functionality. Here I have crypto API and a table where data lies,

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信