Answer the question
In order to leave comments, you need to log in
Why is React waiting for data to load?
useFetch.js
export const useFetch = () => {
const [loading, setLoading] = useState(true)
const [data, setData] = useState([])
const getProducts = async () => {
const response = await fetch(url)
const data = await response.json();
setData(paginate(data));
setLoading(false);
};
useEffect(() => {
getProducts()
}, [])
return { loading, data }
}
const paginate = (followers) => {
const itemsPerPage = 9;
const pages = Math.ceil(followers.length / itemsPerPage) ;
return Array.from({length:pages}, (_, index) => {
const start = index * itemsPerPage;
return followers.slice(start, start + itemsPerPage)
});
}
export default paginate
import paginate from "./utils";
function App() {
const {loading, data} = useFetch();
const [page, setPage] = useState(0);
const [followers, setFollowers] = useState([]);
useEffect(()=>{
if(loading) return;
setFollowers(data[page])
},[loading])
Answer the question
In order to leave comments, you need to log in
const {loading, data} = useFetch();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question