简体   繁体   English

分页会针对所有视图进行渲染,但是应该仅在ReactJS的主页上进行渲染?

[英]Pagination gets rendered for all views but it should be rendered only on home page in ReactJS?

I have created 3 components: 我创建了3个组件:

  1. content 内容
  2. matchinfo matchinfo
  3. layout 布局
  4. pagination 分页

Whenever I click on view stats button matchinfo component view is rendered which displays matchinfo of particular match. 每当我单击视图统计信息按钮时,都会显示matchinfo组件视图,其中显示特定匹配项的matchinfo。 Whenever I click on view stats button (see screenshot) it also renders pagination component also which should not be rendered how can I fix this. 每当我单击“ view stats button (请参见屏幕截图)时,它还会呈现分页组件,该组件不应呈现,我该如何解决。

Component matchinfo is child component of content component. 组件matchinfo是内容组件的子组件。

content.js : content.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,
        loading:false
      });
    })
    }

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

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

    renderMatches() {
        return this.state.matches.slice(this.props.start, this.props.end).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 <div>Loading...</div>
      }
      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;

matchinfo.js : matchinfo.js:

import React, { Component } from 'react';

class Matchinfo extends Component {
    constructor(props){
        super(props);
        this.state = {
            info:[],
            loading:true
        };
    }

    componentWillMount(){
        fetch(`api/match/${this.props.match_id}`)
        .then(res => res.json())
        .then(res => {
      console.log(res)
      this.setState({
        info:res,
        loading:false
      })
    })
    }

  render() {

    if (this.state.loading) {
            return <div>Loading...</div>
    }

    const {info} = this.state;

    return (
      <div>
                         <p className="match">MATCH {info.id}</p>
                        <h4>{info.team1}</h4>
                        <p>VS</p>
                        <h4>{info.team2}</h4>
                        <div className="winner">
                            <h3>WINNER</h3>
                            <h4>{info.winner}</h4>
                        </div>

      </div>
    );
  }
}

export default Matchinfo;

pagination.js : pagination.js:

import React, { Component } from 'react';

class Pagination extends Component {

    handleClick(val){
    this.setState({
        end:val*16,
        start:end-16
    });
    const end = val*16;
    this.props.onChange(end - 16, end);
    }

  render() {

    return (
      <div>
        <div className="container">                 
              <ul className="pagination">
                <li><a href="#" onClick={this.handleClick.bind(this, 1)}>1</a></li>
                <li><a href="#" onClick={this.handleClick.bind(this, 2)}>2</a></li>
                <li><a href="#" onClick={this.handleClick.bind(this, 3)}>3</a></li>
                <li><a href="#" onClick={this.handleClick.bind(this, 4)}>4</a></li>
                <li><a href="#" onClick={this.handleClick.bind(this, 5)}>5</a></li>
              </ul>
          </div>
      </div>
    );
  }
}

export default Pagination;

layout.js : layout.js:

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

class Layout extends Component {
    constructor(props){
        super(props);
        this.state = {
        start:0,
        end:16
        };
    }

    onChangePagination = (start, end) => {
        this.setState({
          start,
          end
        });
    };


    render() {

    const {start, end} = this.state;
    return (
      <div>
          <Content start={start} end={end}/>
          <Pagination onChange={this.onChangePagination}/>
      </div>
    );
  }
}

export default Layout;

Screenshots: 截图:

在此处输入图片说明

Onclick view stats button it still shows pagination: Onclick视图统计信息按钮仍显示分页:

You should approach toggling pagination the same way you did for showing match information. 您应该以与显示匹配信息相同的方式来处理分页。 Hold a variable in this.state for your Layout component and make a method that will control that this.state variable. 在您的Layout组件的this.statethis.state一个变量,并创建一个方法来控制this.state变量。 Pass that method down to your child component. 将该方法传递给您的子组件。 Here is a barebones example: 这是一个准系统示例:

class Layout extends Component {
  constructor(props){
    super(props);
    this.state = {
      showPagination: true
    }
  }

  onChangePagination = () => {
    this.setState({showPagination: !this.state.showPagination}) //toggles pagination
  };

  render() {
    return (
      <div>
        {
          this.state.showPagination
          ?
          <Pagination onChangePagination={this.onChangePagination}/>
          :
          <button onClick={this.onChangePagination}>
            show pagination
          </button>
        }

      </div>
    )
  }
}

class Pagination extends Component {
  handleClick() {
    this.props.onChangePagination()
  }

  render() {
    return (
      <div>
        <button onClick={this.handleClick}>
          toggle pagination
        </button>
      </div>
    )
  }
}

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

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