chore: Remove params requirement on fetchPage

This commit is contained in:
Tom Moor
2024-03-17 13:13:23 -04:00
parent 8bd0aa43bc
commit 4e47f4c1e2
4 changed files with 12 additions and 4 deletions

View File

@@ -16,10 +16,12 @@ type RequestResponse<T> = {
* A hook to make an API request and track its state within a component.
*
* @param requestFn The function to call to make the request, it should return a promise.
* @param makeRequestOnMount Whether to make the request when the component mounts.
* @returns An object containing the request state and a function to start the request.
*/
export default function useRequest<T = unknown>(
requestFn: () => Promise<T>
requestFn: () => Promise<T>,
makeRequestOnMount = false
): RequestResponse<T> {
const isMounted = useIsMounted();
const [data, setData] = React.useState<T>();
@@ -48,5 +50,11 @@ export default function useRequest<T = unknown>(
return undefined;
}, [requestFn, isMounted]);
React.useEffect(() => {
if (makeRequestOnMount) {
void request();
}
}, [request, makeRequestOnMount]);
return { data, loading, error, request };
}