Answer the question
In order to leave comments, you need to log in
How to render json in React?
How to render it?
[{"title":"Lokomotiv","country":"Russia","playerDTO":[{"id":1,"name":"Krivo","surName":"Ruki","gender":"female","bornDate":"1999-10-10T00:00:00"}]}]
import React, { Component } from 'react';
export class AllPlayers extends Component {
constructor(props) {
super(props);
this.state = { forecasts: [], loading: true };
}
componentDidMount() {
this.populateWeatherData();
}
static renderForecastsTable(forecasts) {
return (
<table className='table table-striped' aria-labelledby="tabelLabel">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
{forecasts.map(forecast =>
<tr key={forecast.id}>
<td>{forecast.name}</td>
<td>{forecast.surname}</td>
<td>{forecast.gender}</td>
<td>{forecast.bornDate}</td>
<td>{forecast.title}</td>
<td>{forecast.country}</td>
</tr>
)}
</tbody>
</table>
);
}
render() {
let contents = this.state.loading
? <p><em>Loading...</em></p>
:AllPlayers.renderForecastsTable(this.state.forecasts);
return (
<div>
<h1 id="tabelLabel" >Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
{contents}
</div>
);
}
async populateWeatherData() {
const response = await fetch('api/team/all');
const data = await response.json();
this.setState({ forecasts: data, loading: false });
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question