【发布时间】:2019-10-26 04:07:19
【问题描述】:
由于我是使用 React 的新手,我有一个问题 - 如何正确保存从 API 调用收到的数据,以便最终生成表格/图表。我需要生成一个表格,所以我可以显示每个软件的主机信息
我的 API 调用:
state= {
hosts: [] ,
}
componentDidMount(){
Axios.get('https://api.jsonbin.io/b/5cffb00158196b429f524168').then(
response => {
this.setState( {hosts: response.data})
console.log(response);
console.log(response.data.host[0].meta.name);
console.log(response.data.host[1].meta.name);
console.log(response.data.host[0].software[0].name)
console.log(response.data.host[0].software[1].name)
}
)
}
当我尝试获取 console.log 中某个元素的数据时,一切正常。当我后来尝试制作地图时,我可以显示每个主机的软件,我没有正确获取数据。我不确定我是否在数据保存或稍后地图功能中出错
我的餐桌试用:
<TableSoftware hosts={this.state.hosts}>h</TableSoftware>
表格软件:
<Table >
<thead>
<tr>
<th className="softname">SoftName</th>
<th className="vendor">Vendor</th>
<th className="CPE">CPE</th>
<th className="CVE">CVE:</th>
<th className="CVSS">CVSS:</th>
<th className="DESC">Desc:</th>
<th className="LINK">Link:</th>
</tr>
</thead>
<tbody>
{this.props.hosts.map(function(hoste, index) {
return(
<tr>
<td>{hoste.software.name}</td>
<td>{hoste.software.vendor}</td>
<td>{hoste.software.cpe}</td>
<td>{hoste.software.vulnerability.cve}</td>
<td>{hoste.software.vulnerability.cvss}</td>
<td>{hoste.software.vulnerability.desc}</td>
<td key={index}><Button variant="link" type="submit" >{hoste.software.vulnerability.link}</Button></td> */}
</tr>)})}
【问题讨论】:
-
你能显示你访问状态的部分吗?你在用
this.state.hosts吗? -
@Elphas 更新
-
您可以尝试使用
console.log(this.props.hosts)来查看您的道具是否正确传递。你使用的子组件是函数还是类? -
它们被正确传递了。我在状态下收到了来自主机的整个阵列。子组件是一个类
-
@Elphas 要求检查它们是否正确传递到
TableSoftware。你能在TableSoftware的渲染方法中检查this.props.hosts吗?
标签: javascript reactjs