为所有视图呈现分页,但它应仅在ReactJS的主页上呈现?

时间:2018-01-30 16:44:23

标签: reactjs

我创建了3个组件:

  1. content
  2. matchinfo
  3. 布局
  4. 分页
  5. 每当我点击view stats按钮时,会显示matchinfo组件视图,其中显示特定匹配的matchinfo。每当我点击view stats button(见屏幕截图)时,它也会渲染分页组件也不应该呈现我如何解决这个问题。

    组件matchinfo是内容组件的子组件。

    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:

    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:

    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:

    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;
    

    截图:

    enter image description here

    点击视图统计按钮,它仍显示分页:

1 个答案:

答案 0 :(得分:1)

您应该以与显示匹配信息相同的方式切换分页。在this.state组件的Layout中保留一个变量,并创建一个控制该this.state变量的方法。将该方法传递给您的子组件。这是一个准系统的例子:

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>
    )
  }
}