Created
August 8, 2020 01:32
-
-
Save marco-souza/79457d0b5ac97db6351ae120f726532b to your computer and use it in GitHub Desktop.
JS/TS useFetch react hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from "react"; | |
export enum FetchStatus { | |
IDLE, | |
LOADING, | |
SUCCESS, | |
FAILURE, | |
CANCELED, | |
} | |
export interface UseFetchState<R> { | |
response?: R; | |
error?: Error; | |
status: FetchStatus; | |
} | |
export function useFetch<Res>(url: RequestInfo, options?: RequestInit): UseFetchState<Res> { | |
const [response, setResponse] = useState<Res>() | |
const [error, setError] = useState<Error>(); | |
const [status, setStatus] = useState<FetchStatus>(FetchStatus.IDLE); | |
useEffect(() => { | |
const abortController = new AbortController(); | |
const signal = abortController.signal; | |
const doFetch = async (): Promise<void> => { | |
setStatus(FetchStatus.LOADING); | |
try { | |
const res = await fetch(url, options); | |
if (!res.ok) throw new Error(res.statusText); | |
if (signal.aborted) return setStatus(FetchStatus.CANCELED); | |
const json: Res = await res.json(); | |
setResponse(json); | |
setStatus(FetchStatus.SUCCESS); | |
} catch (e) { | |
if (signal.aborted) return setStatus(FetchStatus.CANCELED); | |
setError(e); | |
setStatus(FetchStatus.FAILURE); | |
} | |
}; | |
doFetch(); | |
return () => { | |
abortController.abort(); | |
}; | |
}, []); | |
return { response, error, status }; | |
} | |
export default useFetch; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment