I've followed many tutorials for how to set up my own custom generic useFetch
hook.
What I came up with works well, but it is breaking some Rules of Hooks.
Mostly, it doesn't use the "correct" set of dependencies.
The generic hook accepts a url, options, and dependencies. Setting the dependencies up as all three creates an infinite refresh loop, even though the dependencies aren't changing.
// Infinite useEffect loop - happy dependencies
const UseRequest: <T>(url: string, options?: Partial<UseRequestOptions> | undefined, dependencies?: any[]) => UseRequestResponse<T>
= <T>(url: string, options: Partial<UseRequestOptions> | undefined = undefined, dependencies: any[] = []): UseRequestResponse<T> => {
const [data, setData] = useState<T | undefined>();
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<UseRequestError | undefined>();
useEffect(() => {
let ignore = false;
(async () => {
try {
setLoading(true);
const response = await (options ? fetch(url) : fetch(url, options))
.then(res => res.json() as Promise<T>);
if (!ignore) setData(response);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
})();
return (() => { ignore = true; });
}, [url, options, dependencies]);
return { data, loading, error };
}
I've found that it works as expected if I omit the options from dependencies (which sort of makes sense as we don't expect this deep object to change in a way we should monitor) and spread the incoming dependencies. Of course, both of these changes break the "Rules of Hooks."
// Working - mad dependencies
const UseRequest: <T>(url: string, options?: Partial<UseRequestOptions> | undefined, dependencies?: any[]) => UseRequestResponse<T>
= <T>(url: string, options: Partial<UseRequestOptions> | undefined = undefined, dependencies: any[] = []): UseRequestResponse<T> => {
const [data, setData] = useState<T | undefined>();
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<UseRequestError | undefined>();
useEffect(() => {
let ignore = false;
(async () => {
try {
setLoading(true);
const response = await (options ? fetch(url) : fetch(url, options))
.then(res => res.json() as Promise<T>);
if (!ignore) setData(response);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
})();
return (() => { ignore = true; });
}, [url, ...dependencies]);
return { data, loading, error };
}
...which I then use like
export const GetStuff: () => UseRequestResponse<Stuff[]> & { refresh: () => void } = () => {
const { appToken } = GetAppToken();
const [refreshIndex, setRefreshIndex] = useState(0);
return {
...UseRequest<Stuff[]>('https://my-domain.api/v1/stuff', {
method: 'GET',
headers: {
'Authorization': `Bearer ${appToken}`
}
}, [appToken, refreshIndex]),
refresh: () => setRefreshIndex(refreshIndex + 1),
};
};
Notice, the only change between the working and broken states was:
}, [url, options, dependencies]);
...to:
}, [url, ...dependencies]);
So, how could I possibly rewrite this to follow the Rules of Hooks and not fall into an infinite refresh loop?
Here is the full code for useRequest
with the defined interfaces:
import React, { useState, useEffect } from 'react';
const UseRequest: <T>(url: string, options?: Partial<UseRequestOptions> | undefined, dependencies?: any[]) => UseRequestResponse<T>
= <T>(url: string, options: Partial<UseRequestOptions> | undefined = undefined, dependencies: any[] = []): UseRequestResponse<T> => {
const [data, setData] = useState<T | undefined>();
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<UseRequestError | undefined>();
useEffect(() => {
let ignore = false;
(async () => {
try {
setLoading(true);
const response = await (options ? fetch(url) : fetch(url, options))
.then(res => res.json() as Promise<T>);
if (!ignore) setData(response);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
})();
return (() => { ignore = true; });
}, [url, ...dependencies]);
return { data, loading, error };
}
export default UseRequest;
export interface UseRequestOptions {
method: string;
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
[prop: string]: string;
},
redirect: string, // manual, *follow, error
referrerPolicy: string, // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: string | { [prop: string]: any };
[prop: string]: any;
};
export interface UseRequestError {
message: string;
error: any;
code: string | number;
[prop: string]: any;
}
export interface UseRequestResponse<T> {
data: T | undefined;
loading: boolean;
error: Partial<UseRequestError> | undefined;
}
dependencies
variable which is to trigger a re-fetch. But you have resolved it in a much better way by using those dependencies to re-instantiate theoptions
object and using theoptions
object as the dependency for the fetch. Great answer. – Cargian