サンプルAPIのデータを表示しようとしています。現時点では私は、配列をループすることができるよ、例えば、すべてのタイトルを表示する:ここでReactでは、HTMLテーブルにAPIからのデータを表示する方法は?
EventTitle1
EventTitle2
EventTitle3
...
は私のコードは次のとおりです。
class App extends React.Component {
constructor() {
super();
this.state = {
data: []
}
}
componentDidMount() {
var th = this;
this.serverRequest = axios.get(this.props.source)
.then(function(event) {
th.setState({
data: event.data
});
})
}
componentWillUnmount() {
this.serverRequest.abort();
}
render() {
var titles = []
this.state.data.forEach(item => {
titles.push(<h3 className=”events”>{item.title[0].value}</h3>);
})
return (
<div className=”container”>
<div className=”row”>
<div className=”col-md-6 col-md-offset-5">
<h1 className=”title”>All Events</h1>
{titles}
</div>
</div>
</div>
);
}
}
ReactDOM.render(
<App source=”http://localhost:8888/example/api/events" />,
document.getElementById(‘container’)
);
どのように私ができますHTMLテーブルを作成して、APIの最新の5つのイベントタイトル(および後で他のデータ)のみを表の中に表示しますか?例えば
:
return (
<table>
<tr>
<th>Event title</th>
<th>Event location</th>
</tr>
<tr>
<td>First event title {titles[0]} ?? </td>
<td>San Fran</td>
</tr>
<tr>
<td>Second event title {titles[1]} ??</td>
<td>London</td>
</tr>
</table>
);
イベントの場所は、APIからではないでしょうか? –
はい、それはAPI – userden