javascript - 如何使用 React 创建分页

标签 javascript git reactjs api pagination

我从 github api 获取数据

我有我需要显示的所有数据,但我想拼接它,这样我每页只能得到 20 个存储库。

而且我不想要一个框架或插件。

总的来说,我对 React 和 JS 还很陌生,所以我不知道从哪里开始或接下来要做什么来创建分页。

import React, {Component} from 'react';
import axios from 'axios';


class Apirequest extends Component {
    constructor(){
         super();
         this.state = {
             githubData: [],
         };
     } 
     componentDidMount() {
       axios.get('https://api.github.com/search/repositories?q=language:javascript&sort=stars&order=desc&per_page=100')
       .then(res => {
         console.log('res', res)
         this.setState({ githubData: res.data.items})
       })
     }

     render() {
       const { githubData } = this.state
         return(
             <div className="container">
                 {githubData.map((name, index) => 
                   <table key={name.id}>
                    <tr>   
                     <th><img src={name.owner.avatar_url}/></th>
                     <td>{name.owner.login}<div className="border-bottom"></div></td>
                     <td>{name.description}<div className="border-bottom"></div></td>
                     <td><a href={name.homepage}>{name.homepage}</a></td>
                    </tr> 
                 </table> 
                 )}
             </div>
         )
     }
 }
export default Apirequest;  

最佳答案

首先,您的map 函数逻辑错误。您正在为每条记录创建一个表,您应该只为每条记录创建一行。 table 标签应该在 map 之外。

 render() {
   const { githubData } = this.state
     return(
         <div className="container">
           <table key={name.id}>
             {githubData.map((name, index) => 
                <tr>   
                 <th><img src={name.owner.avatar_url}/></th>
                 <td>{name.owner.login}<div className="border-bottom"></div></td>
                 <td>{name.description}<div className="border-bottom"></div></td>
                 <td><a href={name.homepage}>{name.homepage}</a></td>
                </tr> 
             )}
           </table> 
         </div>
     )
 }

对于分页,您可以使用 Array.prototype.slice() 来限制显示的行数.只是为了给你一个想法,我发布了一个小例子。您可能需要为该逻辑实现更多功能才能处理您的代码。

示例

previousPage = () => {
  if (this.state.currentPage !== 1)
    this.setState((prevState) => ({currentPage: (prevState.currentPage - 1)}))
}

nextPage = () => {
  if (this.state.currentPage + 1 < this.state.githubData.lenght)
    this.setState((prevState) => ({currentPage: (prevState.currentPage + 1)}))
}

 render() {
   const { githubData, currentPage } = this.state
     return(
         <div className="container">
           <table key={name.id}>
             {githubData.slice((currentPage * 20), 20).map((name, index) => 
                <tr>   
                 <th><img src={name.owner.avatar_url}/></th>
                 <td>{name.owner.login}<div className="border-bottom"></div></td>
                 <td>{name.description}<div className="border-bottom"></div></td>
                 <td><a href={name.homepage}>{name.homepage}</a></td>
                </tr> 
             )}
           </table>
           <button onClick={this.previousPage}>Previous Page</button>
           <button onClick={this.nextPage}>Next Page</button>
         </div>
     )
 }

关于javascript - 如何使用 React 创建分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47080932/

相关文章:

reactjs - 创建钩子(Hook)时真的需要导入 'React'吗? ( react 钩子(Hook))

javascript - Javascript 中的全局变量生命周期

javascript - 使用 Javascript 插入 iFrame

javascript - 当管理员单击菜单时,如何在管理仪表板的颜色区域中激活背景颜色,它会进入默认设置的背景颜色

git - SourceTree 如何重置已推送的提交

linux - 使用反引号时如何抑制命令输出?

javascript - jquery 单选按钮的验证规则

javascript - Firebase 3 身份验证 + Node + jsonwebtoken : Invalid custom token

git - 有没有办法在无缝提交 py 文件而不是 ipynb 时一起使用 Git 和 Jupyter 笔记本?

javascript - 如何使用文本字段名称和值访问动态数组中的正确对象?