[英]How to fetch more data and store it in the state correctly in React?
我想從我的 api 中獲取更多數據。 我正在使用擴展運算符,但它沒有以正確的形式添加到 state 中。
獲取后我得到了這個數組。 在 array[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]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.