如何在ReactJS中的onClick按钮时呈现不同的视图?

时间:2018-01-27 13:41:56

标签: reactjs

我制作了一个名为content的组件,显示了16个匹配项的匹配详细信息。现在有一个名为view stats的按钮,onclick应该呈现不同的视图,显示该特定匹配的详细信息。我怎样才能在ReactJs中做到这一点。有关详细说明,请参见屏幕截图

内容组件:

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),
        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" />
        }

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

export default Content;

在必须在点击事件中呈现的matchinfo组件中:

import React, { Component } from 'react';

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

    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 <img src="https://upload.wikimedia.org/wikipedia/commons/b/b1/Loading_icon.gif" alt=""/>
    }

    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;

截图:

enter image description here

1 个答案:

答案 0 :(得分:3)

Content组件中的render方法添加第二个条件,以检查是否单击了按钮并加载Matchinfo而不是匹配

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