【发布时间】:2020-04-08 10:07:48
【问题描述】:
我是 react js 的新手,我有 json 数据,我想在图像的比较表中显示这个 json api,但我不知道该怎么做我在这里尝试的是我想要的 json api在表中使用http://www.mocky.io/v2/5e8da1cf310000bf90429a90,这是我想显示的图像
这是我尝试过的代码
export class Recommend extends Component {
constructor(props) {
super(props);
this.state = {
main: [],
engine: []
};
}
async componentDidMount(e) {
const res = await fetch(`https://www.mocky.io/v2/5e8d9b243100007a54429a4e`);
const main = await res.json();
this.setState({
main,
engine: main.engine
});
}
render() {
return (
<div className="engine ">
<table className="table ">
{this.state.engine.map(c => (
<tbody>
<tr>
<th>Name</th>
<td>{c.displacement}</td>
</tr>
</tbody>
))}
</table>
</div>
);
}
}
export default Recommend;
【问题讨论】:
-
发布你的完整代码,这样更容易理解你想要做什么。
-
将 和
的东西移到 map 方法之外,就在 标记之后。使用 map 方法只对 json 进行迭代,
我添加了完整代码@Tarreq您的代码逻辑有效,这是一个有效的代码示例,您到底想要什么?!示例工作代码:codesandbox.io/s/…
标签: javascript reactjs html-table compare