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>
}
}
添加回答
舉報