【发布时间】:2020-03-30 19:27:39
【问题描述】:
我是新来的反应,现在正在开发一个必须从 API 获取一些数据的应用程序。
我的组件应该获取数据并取回 Json 对象并填充表格。
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} />
我的库是一个数组,我在我的主应用程序容器中初始化为一个空数组。 路由路径在我的主应用程序中的渲染下。 非常感谢任何帮助。
【问题讨论】:
-
您必须映射整个状态才能使其工作。
标签: json ajax reactjs api router