【发布时间】:2018-10-09 04:43:34
【问题描述】:
在 Tournaments.js 中,我有一个锦标赛名称列表,每个名称都具有从 API 获取的唯一 ID。现在,每当我点击其中一个锦标赛名称时,我都会得到它的 ID,但我需要将此 ID 传递给 Template.js,在那里我可以根据所点击的锦标赛 ID 获取锦标赛数据。我正在尝试将道具从孩子传递给父母,但我现在完全迷失了。
Tournament.js:
import React, { Component } from "react";
import Template from './template';
const API = 'http://localhost:8080/api/tournaments';
class Tournaments extends Component {
constructor() {
super();
this.state = {
data: []
}
}
componentDidMount() {
fetch(API)
.then((Response) => Response.json())
.then((findresponse) => {
console.log(findresponse)
this.setState({
data:findresponse,
})
})
}
reply_click(event) {
var targetId = event.target.getAttribute('id');
console.log(targetId);
}
render() {
return(
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="jumbotron text-center">
{
this.state.data.map((dynamicData, key) =>
<div>
<a href={"/#/template"} id={dynamicData.id} onClick={this.reply_click}>{dynamicData.name}</a>
<a href={"/#/addTeams"}><button type="button" class="btn-lisa">Edit</button></a>
<Template name={dynamicData.id}></Template>
</div>
)
}
</div>
</div>
</div>
</div>
)
}
}
export default Tournaments;
模板.js:
import React, { Component } from "react";
import Parser from 'html-react-parser';
import Tournaments from "./Tournaments";
import './template.css';
import './index.css';
const tournyAPI = 'http://localhost:8080/api/tournaments';
const teamAPI = 'http://localhost:8080/api/teams'
class template extends Component {
constructor() {
super();
this.state = {
data: [],
}
}
componentDidMount() {
fetch(tournyAPI)
.then((Response) => Response.json())
.then((findresponse) => {
this.setState({
tournydata:findresponse.filter(res => res.id === 18),
})
})
所以基本上我的目标是使用 Tournament.js 中的 targetID 代替 Template.js 中 ComponentDidMount 中的“18”
【问题讨论】:
标签: reactjs api react-router react-props