admin管理员组文章数量:1328599
I'm trying to display data from my example API. At the moment I'm able to loop through the array, and display all the titles, for example:
EventTitle1
EventTitle2
EventTitle3
...
Here is my code:
class App extends React.Component {
constructor() {
super();
this.state = {
data: []
}
}
ponentDidMount() {
var th = this;
this.serverRequest = axios.get(this.props.source)
.then(function(event) {
th.setState({
data: event.data
});
})
}
ponentWillUnmount() {
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’)
);
I'm trying to display data from my example API. At the moment I'm able to loop through the array, and display all the titles, for example:
EventTitle1
EventTitle2
EventTitle3
...
Here is my code:
class App extends React.Component {
constructor() {
super();
this.state = {
data: []
}
}
ponentDidMount() {
var th = this;
this.serverRequest = axios.get(this.props.source)
.then(function(event) {
th.setState({
data: event.data
});
})
}
ponentWillUnmount() {
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’)
);
How could I create an HTML table, to display only the five latest event titles from the API (and later other data) inside the table?
For example:
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>
);
Share
Improve this question
asked Oct 25, 2016 at 8:15
userdenuserden
1,6937 gold badges27 silver badges54 bronze badges
2
- Event location is not from the API? – Pranesh Ravi Commented Oct 25, 2016 at 8:19
- Yes, that also es from the API – userden Commented Oct 25, 2016 at 8:19
3 Answers
Reset to default 2Create an array of <tr>
and add it into the table.
class App extends React.Component {
constructor() {
super();
this.state = {
data: []
}
}
ponentDidMount() {
var th = this;
this.serverRequest = axios.get(this.props.source)
.then(function(event) {
th.setState({
data: event.data
});
})
}
ponentWillUnmount() {
this.serverRequest.abort();
}
render() {
const contents = this.state.data.forEach(item => {
// change the title and location key based on your API
return <tr>
<td>{item.title}</td>
<td>{item.location}</td>
</tr>
})
return (
<div className="container">
<div className="row">
<div className="col-md-6 col-md-offset-5">
<h1 className="title">All Events</h1>
<table>
<tr>
<th>Event title</th>
<th>Event location</th>
</tr>
{contents}
</table>
</div>
</div>
</div>
);
}
}
ReactDOM.render(
<App source="http://localhost:8888/example/api/events" />,
document.getElementById("container")
);
You can simply use the slice
operator to get the first 5 elements of you array, and then directly use map
.
There is also no need to use a titles
array, you can just put the snippet inline in the jsx.
The important part of the code is:
<table>
{data.slice(0,5).map(event => (<tr>
<td>{event.title}</td>
<td>{event.location}</td>
</tr>))}
</table>
Here is the full example:
class App extends React.Component {
constructor() {
super();
this.state = {
data: []
}
}
ponentDidMount() {
var th = this;
this.serverRequest = axios.get(this.props.source)
.then(function(event) {
th.setState({
data: event.data
});
})
}
ponentWillUnmount() {
this.serverRequest.abort();
}
render() {
return (
<div className=”container”>
<div className=”row”>
<div className=”col-md-6 col-md-offset-5">
<h1 className=”title”>All Events</h1>
<table>
<tr>
<th>Event title</th>
<th>Event location</th>
</tr>
{this.state.data.slice(0,5).map(event => (<tr>
<td>{event.title}</td>
<td>{event.location}</td>
</tr>))}
</table>
</div>
</div>
</div>
);
}
}
ReactDOM.render(
<App source=”http://localhost:8888/example/api/events" />,
document.getElementById(‘container’)
);
I just would not code it in the above examples like that.
- I would not use setting variables to
this
reminds me the old days of angularjs which a lot of people didvar vm = this
(vm for view model ) Need to be using modern javascript with arrow functions and use
map
and notforEach
sample below:
class App extends React.Component { constructor(props) { super(props); this.state = { data: [] } } ponentDidMount() { axios.get(this.props.source) .then((event) => { this.setState({ data: event.data }); }) } render() { const yourData = this.state.data.map(item => ( // notice array so no return here , ( ) instead of { } // notice also map instead of forEach creates a closure - map is preferred <tr> <td>{item.title}</td> <td>{item.location}</td> </tr> }) return ( <div className=”container”> <div className=”row”> <div className=”col-md-6 col-md-offset-5"> <h1 className=”title”>All Events</h1> <table> <tr> <th>Event title</th> <th>Event location</th> </tr> {yourData} </table> </div> </div> </div> ); } } ReactDOM.render( <App source=”http://localhost:8888/example/api/events" />, document.getElementById(‘container’) ); // above obviously works and old way of doing things, and that is for a div id of containter // I would setup an api to append to and use the ponentDidMount() {... } // then --> export default App;
本文标签: javascriptIn Reacthow to show data from API in a HTML tableStack Overflow
版权声明:本文标题:javascript - In React, how to show data from API in a HTML table? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742225442a2436227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论