【问题标题】:Pagination gets rendered for all views but it should be rendered only on home page in ReactJS?为所有视图呈现分页,但它应该只在 ReactJS 的主页上呈现?
【发布时间】:2018-01-30 16:44:23
【问题描述】:

我创建了 3 个组件:

  1. 内容
  2. 比赛信息
  3. 布局
  4. 分页

每当我点击查看统计按钮时,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;

分页.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;

截图:

Onclick 查看统计按钮仍然显示分页:

【问题讨论】:

  • 处理分页的组件是什么?我没看到。
  • @Andrew 我已经编辑了我的问题,请看。
  • 你永远不会处理你的 组件的渲染...
  • @Andrew 我正在将 props 传递给 layout.js 中的分页
  • @Andrew 我如何处理分页组件的渲染,因为无论它总是被渲染的视图是什么。

标签: reactjs


【解决方案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>
    )
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2018-09-17
    • 2011-03-30
    相关资源
    最近更新 更多