I am new to react and now am working on a app that has to fetch some data from an API.
I have my component that supposed to fetch the data and get an Json object back and populate a table.
class RenderTable() extends React.Component {
render() {
fetch("http://localhost:6002/api?act=getall")
.then(data=> data.json())
.then(
result => {
this.setState({
library: result
});
console.log(this.state.result);
},
error => {
this.setState({
library: "error"
});
return null;
}
);
}
render() {
return (
<div id="libTable">
<table>
<tbody>
<tr>
<th>Genre</th>
<th>Description</th>
<th>Date</th>
<th>Price</th>
</tr>
<tr>
<td>{JSON.stringify(this.state.library)}</td>
</tr>
</tbody>
</table>
</div>
);
} }
<Route path="/RenderTable" component={RenderTable} />
my library is an array that I initialize as an empty array in my main app container. The routing path is in my main app under render. Any help is much appreciated.