gpt4 book ai didi

reactjs - ReactJS中onClick按钮时如何呈现不同的 View ?

转载 作者:行者123 更新时间:2023-12-03 13:44:11 26 4
gpt4 key购买 nike

我制作了名为 content 的组件,它显示 16 场比赛的比赛详细信息。现在有一个名为“查看统计信息”的按钮,单击该按钮应呈现不同的 View ,显示该特定比赛的详细信息。我怎样才能在 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

最佳答案

Content 组件中的渲染方法中添加第二个条件,以检查是否单击了按钮并加载 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>
);
}

关于reactjs - ReactJS中onClick按钮时如何呈现不同的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48476400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com