Skip to content

Instantly share code, notes, and snippets.

@AdsonCicilioti
Last active March 7, 2021 01:00
Show Gist options
  • Save AdsonCicilioti/2640fb7d9d191d9d925d3348d0de52e5 to your computer and use it in GitHub Desktop.
Save AdsonCicilioti/2640fb7d9d191d9d925d3348d0de52e5 to your computer and use it in GitHub Desktop.
Hook SWR Typescript
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