如何在React中正确获取更多数据并将其存储在状态中?



我想从我的api获取更多的数据。我正在使用扩展运算符,但它没有以正确的形式添加到状态。

取回后,我得到这个数组。在数组[5]上,它添加了一个额外的数组。相反,我想让它添加到现有的

0: {route_id: 7, price: 16760, distance: 111, shift_time: 20, max_load: 250, …}
1: {route_id: 2, price: 0, distance: 0, shift_time: 0, max_load: 100, …}
2: {route_id: 3, price: 0, distance: 0, shift_time: 0, max_load: 55, …}
3: {route_id: 4, price: 0, distance: 111, shift_time: 20, max_load: 240, …}
4: {route_id: 1, price: 0, distance: 0, shift_time: 0, max_load: 57, …}
5: (5) [{…}, {…}, {…}, {…}, {…}]

我代码:

axios(config)
.then(function (response) {

setRefreshing(false)
setCardLoading(false)
console.log(response.data);
if(response.data.status_code === 200) {
setRoutes([...routes, response.data.response])
}
})

似乎你也需要传播你的回应,如:

setRoutes([...routes, ...response.data.response]);

最新更新