[英]ReactJS Json Object array
I am having an issue mapping some JSON:我在映射一些 JSON 时遇到问题:
https://environment.data.gov.uk/flood-monitoring/id/floodAreas/?county=Lincolnshire https://environment.data.gov.uk/flood-monitoring/id/floodAreas/?county=Lincolnshire
Would really appreciate the help, thanks非常感谢您的帮助,谢谢
I am using the following code to attempt to map the json object我正在使用以下代码尝试 map json object
await fetch('https://environment.data.gov.uk/flood-monitoring/id/floodAreas/?county=Lincolnshire')
.then(res => res.json())
.then(json => {
this.setState({
hasLoaded: true,
weatherItems: json
})
});
static renderFloodTable(weatherItems) {
return (
<table class="centerTable" className='table table-striped' aria-labelledby="tabelLabel">
<thead>
<tr>
<th>County. (C)</th>
</tr>
</thead>
<tbody>
{weatherItems.items.map(item =>
<tr key={item.fwdCode}>
<td>{item.fwdCode}</td>
</tr>
)}
</tbody>
</table>
);
}
i think the rendering in calling too soon so the weatherItems
is'nt set yet.我认为调用中的渲染太早了,所以weatherItems
还没有设置。 try this:试试这个:
{weatherItems? weatherItems.items.map( //... etc... // ): null}
and btw, you should not use await
And .then
toogether... choose one...顺便说一句,你不应该使用await
And .then
toogether...选择一个...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.