I have two classes club, clubs and the main app class. In the clubs class i'm getting a list of club and I am displaying them in a <ul> list. In the club class I'm trying the get the detail of the clicked item from the clubs list. The point is I don't know how to call the detail function present in the club class in the clubs class.
Here's the details:
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;
In the onClick prop, I've made this call {Club.clubDetail(item.id)} but it seems not working
The main app class:
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;