[英]React.js: Uncaught TypeError: response.data.posts is not iterable
我正在創建一個無限漫步,當嘗試將新數據添加到 state 中時,現有數據 Uncaught TypeError: response.data.posts is not iterable 出現
const [posts, setPosts] = useState([]) //my state
setPosts((prev) => [...prev, ...response.data.posts]);// the way i'm trying to add the data
我的 api 結構
posts: {
current_page: 1
data: [{id: 1, title: "Nobis iste sed ullam deleniti quis excepturi
nemo.",…},…]
first_page_url: "http://127.0.0.1:8000/api/front/all-posts?page=1"
from: 1
last_page: 15
last_page_url: "http://127.0.0.1:8000/api/front/all-posts?page=15"
links: [{url: null, label: "« Previous", active: false},…]
next_page_url: "http://127.0.0.1:8000/api/front/all-posts?page=2"
path: "http://127.0.0.1:8000/api/front/all-posts"
per_page: 10
prev_page_url: null
to: 10
total: 150
success: true
}
似乎帖子是您的 API 結構中的對象,這就是您遇到可迭代錯誤的原因。 你試過了嗎
setPosts((prev) => [...prev, ...response.data.posts.data]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.