Skip to content

Instantly share code, notes, and snippets.

@ashimon83
Last active June 7, 2021 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashimon83/52fbd19a877d3b9ea88e1f5c380dca2e to your computer and use it in GitHub Desktop.
Save ashimon83/52fbd19a877d3b9ea88e1f5c380dca2e to your computer and use it in GitHub Desktop.
fetch with hooks
const ComponentWithUseFetch = () => {
const { data, isLoading, hasError, errorMessage, refetch } = useFetch<{ name: string }>({
url: '/api/someone/123',
skip: true
})
if (isLoading) {
return <Loader />
}
return (
<>
<div>
{data ? `Hello ${data.name} !` : 'please click button'}
</div>
{hasError && <b>{errorMessage}</b>}
<button onClick={() => refetch()}>call user name</button>
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment