This guide is to show you how to create a simple react hook for data fetching (with revalidation).
🤨 Why this hook?
When fetching data ...
For further actions, you may consider blocking this person and/or reporting abuse
Cool Work, But I'm agree with one of the user that could you add some cancel request.
Nice! Working with graphql I saw a hook called
useQuery
which returnsloading
,data
,error
andrefetch
.So, after some attempts to decouple the gql or rest implementation we've created a hook very similar to your
useFetch
which returns the four variables like theuseQuery
.I think that the
loading
flag could be useful for this hook too. You know, we can infer it if thedata
has a value but theloading
will help you to know if there is a "fetching" task in progressYeah, I kept thinking whether I should add a loading value or not, I initially did but I removed it, You can contribute to the repo.
Isn' that what SWR does? Why not go with Standards?
It’s always fun to build and know how things work
I agree. But for production it's mostly better to go with the standards 🤗
From quick look of example code, it will show loading state for infinite time, if your api call fails.
It sets
error
to true, which displaysAre you sure? It looks like
data
would be falsy (if the fetch errors out!) and so your second if block can never run.git clone
the repo and look for yourself, if this is really an issue please add anissue
to the repo or try to contribute to it 💜Thank you
Can u add cancel request when components is unmount
Hmmm, Could you add it as a GitHub issue