Last active
March 7, 2021 01:00
-
-
Save AdsonCicilioti/2640fb7d9d191d9d925d3348d0de52e5 to your computer and use it in GitHub Desktop.
Hook SWR Typescript
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 useSWR, { ConfigInterface, responseInterface } from 'swr' | |
export const fetcher = async (url: string): Promise<any> => { | |
const response = await fetch(url) | |
const data = await response.json() | |
return data | |
} | |
export function useFetch<Data = any, Error = any>( | |
url: string, | |
config?: ConfigInterface | |
): responseInterface<Data, Error> { | |
const { data, error, mutate, isValidating, revalidate } = useSWR<Data, Error>( | |
url, | |
fetcher, | |
{ | |
revalidateOnFocus: false, | |
...config | |
} | |
) | |
return { data, error, mutate, isValidating, revalidate } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment