Я удалил stati c из renderTeamsTable
, а затем вызвал метод как this.renderTeamsTable(this.state.teams);
Это позволило onClick
вызываться, как и ожидалось
Обновленный код
export class FetchTeamData extends Component {
displayName = FetchTeamData.name
constructor(props) {
super(props);
this.state = { teams: [], loading: true, showComponent: false, selectedTeam: [] };
fetch('api/team')
.then(response => response.json())
.then(data => {
this.setState({ teams: data, loading: false });
});
}
selectTeamOnClick(team) {
this.setState({
showComponent: true,
loading: false,
selectedTeam: team
});
}
renderTeamsTable(teams) {
return (<table className='table'>
<thead>
<tr>
<th>Team</th>
<th>Division</th>
<th>Conference</th>
</tr>
</thead>
<tbody>
{teams.map(team =>
<tr key={team.id} onClick={() => this.selectTeamOnClick(team.team)} >
<td>{team.team}</td>
<td>{team.division}</td>
<td>{team.conference}</td>
</tr>
)}
</tbody>
</table>)
}
render() {
let contents = this.state.loading
? <p><em>Loading...</em></p>
: this.renderTeamsTable(this.state.teams);
return (
<div>
<h1>NBA Teams</h1>
<p>All current teams in NBA</p>
{this.state.showComponent ?
<button onClick={() => this.setState({ showComponent: false })}>
Clear Players</button> :
null
}
{this.state.showComponent ?
<FetchPlayerFromTeamData team={this.state.selectedTeam} /> :
null
}
{contents}
</div>
);
}
}