【发布时间】:2017-03-07 04:41:09
【问题描述】:
我正在尝试显示来自我的示例 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 的五个最新事件标题(以及后来的其他数据)?
例如:
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
标签: javascript reactjs