AllenYuan

pagination - listpagination component

Apr 19th, 2020
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. https://github.com/yuana97/code-snippets/tree/master/react-redux-realworld
  2.  
  3. create src/components/ListPagination.js
  4.  
  5. import React from 'react';
  6.  
  7. const ListPagination = props => {
  8.   if (props.articlesCount <= 10) {
  9.     return null;
  10.   }
  11.  
  12.   const range = [];
  13.   for (let i = 0; i < Math.ceil(props.articlesCount / 10); ++i) {
  14.     range.push(i);
  15.   }
  16.  
  17.   const setPage = page => props.onSetPage(page);
  18.  
  19.   return (
  20.     <nav>
  21.       <ul className="pagination">
  22.  
  23.         {
  24.           range.map(v => {
  25.             const isCurrent = v === props.currentPage;
  26.             const onClick = ev => {
  27.               ev.preventDefault();
  28.               setPage(v);
  29.             };
  30.             return (
  31.               <li
  32.                 className={ isCurrent ? 'page-item active' : 'page-item' }
  33.                 onClick={onClick}
  34.                 key={v.toString()}>
  35.  
  36.                 <a className="page-link" href="">{v + 1}</a>
  37.  
  38.               </li>
  39.             );
  40.           })
  41.         }
  42.  
  43.       </ul>
  44.     </nav>
  45.   );
  46. };
  47.  
  48. export default ListPagination;
Advertisement
Add Comment
Please, Sign In to add comment