Skip to content

Instantly share code, notes, and snippets.

@cdaz5
Created April 30, 2020 21:11
Show Gist options
  • Save cdaz5/76e8fb18f6824a5f11ec015949286591 to your computer and use it in GitHub Desktop.
Save cdaz5/76e8fb18f6824a5f11ec015949286591 to your computer and use it in GitHub Desktop.
useEffect(() => {
let next = url;
let plans = [];
const fetch = async (url) => {
try {
setLoading(true);
const { data } = await axios(url);
next = data.next;
return data.results;
} catch (error) {
setError(error);
} finally {
setLoading(false);
}
};
const recursiveFetch = async () => {
while (next) {
const res = await fetch(next);
plans = [...plans, ...res];
}
setData(plans);
};
recursiveFetch();
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment