【问题标题】:How to slice JSON data in ReactJS?如何在 ReactJS 中对 JSON 数据进行切片?
【发布时间】:2018-07-08 06:14:02
【问题描述】:

我制作了 2 个组件 1)Content.js 2)Pagination.js 我想在用户单击分页组件时显示内容。每页共有 16 个对象。我在第 1 页使用了 .slice(0,16) 方法。现在我想根据页码更改切片函数中传递的参数,例如:
.slice(0,16) --> 第 1 页 .slice(17,33) ---> 第 2 页 依此类推,直到第 5 页,每页应显示 16 个对象,共 5 页。

内容.js:

import React, { Component } from 'react';
import Matchinfo from './matchinfo';
import './content.css';

class Content extends Component {
    constructor(props){
        super(props);
        this.state = {
            matches:[],
            loading:true,
            callmatchinfo: false,
            matchid:''
        };
    }

    componentDidMount(){
        fetch('api/matches')
        .then(res => res.json())
        .then(res => {
      console.log(res)
      this.setState({
        matches:res.slice(0,16),     <----Parameters inside it must be changed for each page
        loading:false
      });
    })
    }

  viewstats(matchid){
    this.setState({
        callmatchinfo: true,
        matchid: matchid
    });
  }

  rendermatchinfo(){
    return <Matchinfo matchid={this.state.matchid} />
  }

    renderMatches() {
        return this.state.matches.map(match => {
            return (
                <div className="col-lg-3">
                    <div id="content">
                        <p className="match">MATCH {match.id}</p>
                        <h4>{match.team1}</h4>
                        <p>VS</p>
                        <h4>{match.team2}</h4>
                        <div className="winner">
                            <h3>WINNER</h3>
                            <h4>{match.winner}</h4>
                        </div>
                        <div className="stats">
                            <button type="button" onClick= {()=>{this.viewstats(match.id)}} className="btn btn-success">View Stats</button>
                        </div>
                    </div>
                </div>
            );
        })
    }

    render() {

        if (this.state.loading) {
            return <img src="https://upload.wikimedia.org/wikipedia/commons/b/b1/Loading_icon.gif" />
        }
        else if(this.state.callmatchinfo){
        return <Matchinfo match_id={this.state.matchid} />
        }

    return (
      <div>
          <div className="row">
            {this.renderMatches()}
              </div>
          <div className="row">
            {this.state.callmatchinfo ? this.rendermatchinfo() : ''}
          </div>
      </div>
    );
  }
}

export default Content;

分页.js:

import React, { Component } from 'react';
import Content from './content';

class Pagination extends Component {

  render() {
    return (
      <div>
          <div className="container">                 
              <ul className="pagination">
                <li><a href="#">1</a></li>
                <li><a href="#">2</a></li>
                <li><a href="#">3</a></li>
                <li><a href="#">4</a></li>
                <li><a href="#">5</a></li>
              </ul>
          </div>

      </div>
    );
  }
}

export default Pagination;

为了更清楚,请参见第 1 页的屏幕截图。此功能必须扩展为 5 页。

【问题讨论】:

    标签: reactjs pagination


    【解决方案1】:

    导入Pagination 组件并附加如下事件:

    import React, { Component } from 'react';
    import Content from './content';
    
    class Pagination extends Component {
    
      render() {
        return (
          <div>
              <div className="container">                 
                  <ul className="pagination">
                    <li onClick={this.props.onPageNext.bind(this, 1)} ><a href="#">1</a></li>
                <li onClick={this.props.onPageNext.bind(this, 2)} ><a href="#">2</a></li>
     <li onClick={this.props.onPageNext.bind(this, 3)} ><a href="#">3</a></li>
                  </ul>
              </div>
    
          </div>
        );
      }
    }
    

    导出默认分页;

    在 Content.js 中:

    ...

    return (
          <div>
              <div className="row">
                {this.renderMatches()}
                  </div>
              <div className="row">
                {this.state.callmatchinfo ? this.rendermatchinfo() : ''}
              </div>
                <Pagination onPageNext={this.onPageNext} /> 
    
          </div>
        );
    

    ...

      onPageNext(nextPage) {
        // logic to set the setState method for pagination...
      }
    

    【讨论】:

    • 我找不到这条线export const Pagination = ({onPageNext})
    • 我建议你不需要 class Pagination extends Component 而是像我建议的那样一个愚蠢的组件来完成这项工作
    • 我不明白什么是哑组件,但这是层次结构内容组件-> 分页组件-> 主页组件。我想做的是使用状态变量 start 和 end 最初 start =0 和 end =16 必须在 .slice(this.props.start,this.props.end) 内传递,然后 onClick 我只想在 .slice() 内注入新参数,例如:如果我点击页面2 它应该注入 start=17 和 end= 33 我该怎么做?
    • 哑组件无法直接访问应用状态。它仅依赖于您作为道具传递的数据,因此名称为“哑”。容器(智能组件)直接从应用状态获取数据。
    • 更新了请检查,你可以在你的Content.js中定义onPageNext事件方法,只要点击分页触发,该方法就会被触发。
    猜你喜欢
    • 2020-12-05
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2010-09-15
    相关资源
    最近更新 更多