Fix loop in useFetch

Oops, my recent change made it so `useFetch` started re-requesting stuff in an infinite loop 😅 right, this is how effects work, lol!
This commit is contained in:
Emi Matchu 2021-04-23 11:48:38 -07:00
parent 0fe83419f7
commit 2f7b6571f2

View file

@ -238,10 +238,15 @@ export function useFetch(url, { responseType, ...fetchOptions }) {
const [error, setError] = React.useState(null);
const [data, setData] = React.useState(null);
// We expect this to be a simple object, so this helps us only re-send the
// fetch when the options have actually changed, rather than e.g. a new copy
// of an identical object!
const fetchOptionsAsJson = JSON.stringify(fetchOptions);
React.useEffect(() => {
let canceled = false;
fetch(url, fetchOptions)
fetch(url, JSON.parse(fetchOptionsAsJson))
.then(async (res) => {
if (canceled) {
return;
@ -265,7 +270,7 @@ export function useFetch(url, { responseType, ...fetchOptions }) {
return () => {
canceled = true;
};
}, [url, fetchOptions]);
}, [url, fetchOptionsAsJson]);
return { loading, error, data };
}