我有两个课程:俱乐部、俱乐部和主要应用程序课程。在俱乐部课程中,我收到一份俱乐部列表,并将它们显示在<ul>
列表。在里面club
类我正在尝试从俱乐部列表中获取单击项目的详细信息。关键是我不知道如何调用中存在的详细函数club
类在clubs
class.
详细信息如下:
The Club class:
import React, { Component } from 'react';
const clubData = id => `urlto`
class Club extends Component {
constructor(props) {
super(props)
this.state = {
requestFailed: false
}
// This binding is necessary to make `this` work in the callback
this.clubDetail = this.clubDetail.bind(this);
}
clubDetail(id) {
console.log('karim')
{/*fetch(clubData(id)
.then(response => {
if (!response.ok) {
throw Error("Failed connection to the API")
}
return response
})
.then(response => response.json())
.then(response => {
this.setState({
club: response
})
}, () => {
this.setState({
requestFailed: true
})
})*/}
}
render() {
if(!this.state.club) return <h1>No club selected !</h1>
return (
<ul>
<li>Name : {this.state.club.name}</li>
<li>Email : {this.state.club.email}</li>
<li>Website : {this.state.club.website}</li>
</ul>
);
}
}
export default Club;
Clubs class:
import React, { Component } from 'react';
const clubsList = `urlto`
class Clubs extends Component {
constructor(props) {
super(props)
this.state = {
requestFailed: false
}
}
componentDidMount() {
fetch(clubsList)
.then(response => {
if (!response.ok) {
throw Error("Failed connection to the API")
}
return response
})
.then(response => response.json())
.then(response => {
this.setState({
clubs: response
})
}, () => {
this.setState({
requestFailed: true
})
})
}
render() {
if(!this.state.clubs) return <h1>No results</h1>
return (
<div>
<ul>
{this.state.clubs.map(function(item) {
return <a key={item.id} onClick={Club.clubDetail(item.id)}><li key={item.id}>{item.name}</li></a>
})}
</ul>
</div>
);
}
}
export default Clubs;
在 onClick 属性中,我进行了此调用{Club.clubDetail(item.id)}
但似乎不起作用
主要应用程序类:
class App extends Component {
render() {
return (
<div className="App">
<div className="App-left-side">
<Clubs></Clubs>
</div>
<div className="App-center-side">
<div className="App-center-side-content">
<Club></Club>
</div>
</div>
</div>
);
}
}
export default App;
Since Clubs
and Club
组件只是用来替换数据,所以我建议将api
来电来自App
组件,并将数据传递给Clubs
组件以及 onClick 函数。每当用户单击任何项目时,将 id 传递给父级并替换所单击项目的详细信息Club
成分。
像这样:
应用程序组件:
const clubsList = `urlto`;
class App extends Component {
constructor(props) {
super(props)
this.state = {
requestFailed: false,
clubs: [],
club: {}
}
}
componentDidMount() {
fetch(clubsList)
.then(response => {
if (!response.ok) {
throw Error("Failed connection to the API")
}
return response
})
.then(response => response.json())
.then(response => {
this.setState({
clubs: response
})
}, () => {
this.setState({
requestFailed: true
})
})
}
click = (id) => {
console.log('clicked item', id);
}
render() {
return (
<div className="App">
<div className="App-left-side">
<Clubs clubs={this.state.clubs} clicked={this.click}/>
</div>
<div className="App-center-side">
<div className="App-center-side-content">
<Club club={this.state.club}/>
</div>
</div>
</div>
);
}
}
export default App;
俱乐部组成部分:
class Clubs extends Component {
render() {
if(!this.props.clubs.length) return <h1>No results</h1>
return (
<div>
<ul>
{this.props.clubs.map((item) => {
return <a key={item.id} onClick={() => this.props.clicked(item.id)}>
<li key={item.id}>{item.name}</li>
</a>
})}
</ul>
</div>
);
}
}
export default Clubs;
俱乐部组成部分:
class Club extends Component {
render() {
if(!Object.keys(this.props.club)) return <h1>No club selected !</h1>;
return (
<ul>
<li>Name : {this.props.club.name}</li>
<li>Email : {this.props.club.email}</li>
<li>Website : {this.props.club.website}</li>
</ul>
);
}
}
export default Club;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)