Question on TanStack Query
Unanswered
Palomino posted this in #help-forum
PalominoOP
hey guys! hope everyones doing great!, so recently i came across TanStack Query which simplifies a lot when it comes to the fetch requests! Im going to be using it from now on but im kind of confused as theres a lot to unpack from the documentation,
I wanted to ask what exactly are the hooks etc that we're gonna be using 90% of the time when it comes to tanstack query? for example useQuery returns a lot of values right? but i dont think we'll ever be using all of them,
for example i dont really get the differences between isFetching, isLoading, isError, isPending? they all seem to be doing the same thing ? when exactly do we use which one for what case?
also about the caching i guess it caches fetch results by default ? how can we make it not cache for example and also im kind of confused about the refetch function that can be added as an onClick to a button for example, what exactly is it used for ?
i was wondering if anyone could breakdown the most useful things from tanstack query. i could learn those and then learn the others that arent used more often!
also i guess tanStack is just for fetch request handling and getting back data right? so for all other state handling we'd have to use redux for example ??
sorry if my questions sound stupid its my first time working with tanstack!
I wanted to ask what exactly are the hooks etc that we're gonna be using 90% of the time when it comes to tanstack query? for example useQuery returns a lot of values right? but i dont think we'll ever be using all of them,
for example i dont really get the differences between isFetching, isLoading, isError, isPending? they all seem to be doing the same thing ? when exactly do we use which one for what case?
also about the caching i guess it caches fetch results by default ? how can we make it not cache for example and also im kind of confused about the refetch function that can be added as an onClick to a button for example, what exactly is it used for ?
i was wondering if anyone could breakdown the most useful things from tanstack query. i could learn those and then learn the others that arent used more often!
also i guess tanStack is just for fetch request handling and getting back data right? so for all other state handling we'd have to use redux for example ??
sorry if my questions sound stupid its my first time working with tanstack!
1 Reply
You can use tanstack query for stage managenent too.
isFetching happens the first fetch , isLoading happens on all fetches iirc, this is useful if you have paginated data, like first fetch you can show "getting your data", and for next pages you show "getting more data", isError will only show fetching errors, so if you get a 400 or 500 response, it won't be considered error, only errors like wrong url path or if he can't reach backend or if he doesn't get a response.
You can start simple and add more of those helpers as you need them
Instead of the refetch function, I would suggest using revalidate on the tags that you want, when you do a patch or a post, and cached data is outdated, you want to show the more recent one, this is why is useful revalidate data
I also recommend using tanstack query dev tools, you can see the state of the data and see when all those status happen
isFetching happens the first fetch , isLoading happens on all fetches iirc, this is useful if you have paginated data, like first fetch you can show "getting your data", and for next pages you show "getting more data", isError will only show fetching errors, so if you get a 400 or 500 response, it won't be considered error, only errors like wrong url path or if he can't reach backend or if he doesn't get a response.
You can start simple and add more of those helpers as you need them
Instead of the refetch function, I would suggest using revalidate on the tags that you want, when you do a patch or a post, and cached data is outdated, you want to show the more recent one, this is why is useful revalidate data
I also recommend using tanstack query dev tools, you can see the state of the data and see when all those status happen