Skip to content

Instantly share code, notes, and snippets.

@pena56
Last active December 3, 2021 22:19
Show Gist options
  • Save pena56/4aebe8733e039f695b9d9f30301199ce to your computer and use it in GitHub Desktop.
Save pena56/4aebe8733e039f695b9d9f30301199ce to your computer and use it in GitHub Desktop.
import { useState, useEffect, useCallback } from 'react';
function useFetch(page) {
const [loading, setLoading] = useState(false);
const [photos, setPhotos] = useState([]);
const getPhotos = useCallback(async () => {
try {
setLoading(true);
const response = await fetch(
`https://picsum.photos/v2/list?page=${page}&limit=10`
);
const data = await response.json();
setPhotos((prev) => [...prev, ...data]);
setLoading(false);
} catch (err) {
console.error(err);
}
}, [page]);
useEffect(() => {
getPhotos();
}, [getPhotos]);
return { loading, photos };
}
export default useFetch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment