Onclick按钮我想在ReactJS中渲染多个组件?

时间:2018-01-26 13:32:13

标签: reactjs

我制作了Matchinfo组件和Navbar组件。当我点击view stats按钮时,它应该呈现Navbar(以便我们可以导航回主页,反之亦然)和Matchinfo组件。

Content.js:

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

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

    componentDidMount(){
        fetch('api/matches')
        .then(res => res.json())
        .then(res => {
      console.log(res)
      this.setState({
        matches:res.slice(0,16),
        loading:false
      })
    })
    }

    renderMatches() {
        return this.state.matches.map(match => {
            return (
                <div class="col-lg-3">
                    <div id="content">
                        <p class="match">MATCH {match.id}</p>
                        <h4>{match.team1}</h4>
                        <p>VS</p>
                        <h4>{match.team2}</h4>
                        <div class="winner">
                            <h3>WINNER</h3>
                            <h4>{match.winner}</h4>
                        </div>
                        <div class="stats">
        Button --->                    <button type="button" class="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 class="row">
            {this.renderMatches()}
          </div>


      </div>
    );
  }
}

export default Content;

单击按钮,它应该呈现2个不同的组件,我该怎么做?

请参阅以下必须呈现的组件:

Navbar组件:

import React, { Component } from 'react';

class Navbar extends Component {

  render() {
    return (
      <div>
          <nav class="navbar navbar-inverse">
            <div class="container-fluid">
                <div class="navbar-header">
                  <a class="navbar-brand" href="#">Cricket App</a>
                </div>
                <ul class="nav navbar-nav">
                  <li><a href="#">Home</a></li>
                </ul>
            </div>
          </nav>
      </div>
    );
  }
}

export default Navbar;

Matchinfo组件:

import React, { Component } from 'react';

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

    componentDidMount(){
        fetch('api/match/: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" />
    }

    const {info} = this.state;

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

      </div>
    );
  }
}

export default Matchinfo;

更多说明的屏幕截图,请参阅查看统计信息按钮(绿色按钮): enter image description here

1 个答案:

答案 0 :(得分:1)

我认为你不需要从app.js调用MatchInfo的路由。检查以下更新的代码。如果您在上一篇文章中提到建议的代码,当您点击查看统计信息时,您会在页面中看到导航栏。它应该工作

此处的流程是您的内容组件显示视图统计信息所以在内容中我通过将matchId作为props传递来调用MaTchInfo组件,而MatchInfo组件将matchId传递给componentDidMount中的fetch api调用。多数民众赞成。

import React, { Component } from 'react';
import './content.css';
import MatchInfo './components/MatchInfo'

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

    componentDidMount(){
        fetch('api/matches')
        .then(res => res.json())
        .then(res => {
      console.log(res)
      this.setState({
        matches:res.slice(0,16),
        loading:false
      })
    })
    }

    viwStats(matchId){
      this.setState({
        callMatchInfo: true,
        matchId: matchId 
      })
    }

    renderMatchInfo(){
      <MatchInfo matchId={this.state.matchId} />
    }

    renderMatches() {
        return this.state.matches.map(match => {
            return (
                <div class="col-lg-3">
                    <div id="content">
                        <p class="match">MATCH {match.id}</p>
                        <h4>{match.team1}</h4>
                        <p>VS</p>
                        <h4>{match.team2}</h4>
                        <div class="winner">
                            <h3>WINNER</h3>
                            <h4>{match.winner}</h4>
                        </div>
                        <div class="stats">
                          <button type="button" onClick={this.viwStats(match.id)} class="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 class="row">
            {this.renderMatches()}
          </div>
          <div class="row">
            {this.state.callMatchInfo ? this.renderMatchInfo() : ''}
          </div>

      </div>
    );
  }
}

export default Content;

AND在您的Matchinfo组件中:this.props.matchId作为获取调用中的请求参数

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