2 回答

TA贡献1868条经验 获得超4个赞
您需要从 response.data 中解构位置:
state = {
locations: []
}
async componentDidMount() {
try {
const response = await axios({
method: 'get',
url: `my API url`,
headers: {
'token': token
}
})
const { locations } = response.data
console.log(locations)
this.setState({ locations })
} catch (e) {
console.log(e)
}
}
render() {
// ...
}

TA贡献1859条经验 获得超6个赞
如果这是数据的形式:
{status: "success", data: {…}}
data:
locations: Array(311)
[0 … 99]
[100 … 199]
[200 … 299]
[300 … 310]
length: 311
__proto__: Array(0)
__proto__: Object
然后像这样设置状态:
state = {
records:{
data:{
location:[]
}
}
您正在接收一个对象。在该对象内部还有另一个称为“数据”的对象,它保存一个位置数组。然后你可以像这样迭代。
render(){
const {records} = this.state;
return{
<div>
{records.data.location.map(l => l.deviceId)} // and so on
</div>
}
}
添加回答
举报