"TypeError: Invalid attempt to destructure non-iterable instance"



我正在使用ionic react来构建一个应用程序。我得到这个错误

TypeError: Invalid attempt to destructure non-iterable instance

运行程序后https://i.ibb.co/HgkNgyT/error.jpg。
这是我的代码

import React, { useEffect, useState } from 'react';
import {
IonContent, IonHeader, IonTitle, IonToolbar, IonApp, IonButton, IonFooter
} from '@ionic/react';
import HomeDatas from '../HomeDatas/HomeDatas';

const Home = () => {
const [currentPage, setCurrentPage] = useState(1);
const [postPerPage, setPostPerPage] = useState(5);
const [cafeData, setCafeData] = useState([]);
useEffect(() => {
fetch('https://api.github.com/search/code?&per_page=15')
.then(res => res.json())
.then(data => setCafeData(data.items))
}, [])
console.log(typeof (cafeData));
const newData = Object.values(cafeData);
//Get current posts
const [indexOfLastPost] = currentPage * postPerPage;
const [indexOfFirstPost] = indexOfLastPost - postPerPage;
const [currentPosts] = newData.slice(indexOfFirstPost, indexOfLastPost)
return (
<IonApp>
<IonHeader translucent>
<IonToolbar>
<IonTitle>App</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent class='ion-justify-content-center'>
{currentPosts.map(data => <HomeDatas data={data} ></HomeDatas>)}
</IonContent>
<IonToolbar>
<IonFooter>
<IonButton>Previous</IonButton>
<IonButton>Next</IonButton>
</IonFooter>
</IonToolbar>
</IonApp>
);
};
export default Home;

你确定吗?返回一个数组?记住,只能解构数组,不能解构int类型

相关内容

最新更新