react-query Flashcards
difference between staleTime and cacheTime
staleTime tell for how long the refetching be avoided. (default = 0ms)
cacheTime tells, for how long isLoading should be set to false (to give optimistic updates) (default = 5 mins)
booleans to indicate api call in progress
isLoading: when there is no cache and api fetching
isFetching: when there is cache, api is fetching
refetchBooleans
refetchOnMount: (default is true)
refetchOnWindowFocus : (default is true)
stock market polling
we can poll data at regular intervals using two option keys
refetchInterval, refetchIntervalInBackground
refetchInterval: default value is false, we can set it to ms, and api will be called every set ms
refetchIntervalInBackground: if the window looses focus the polling stops, we can keep the polling on, if by using this key.
default value is false, we can set, it to ms.
Fetch API on user event
2 step process
1. set option enabled:false
2. call refetch on user event.
callbacks
there is onSuccess callback which gets data
there is onError callback which gets error object
data transformation
we can use select
option which accepts a selector fn.
selector fn will get data as input argument
if you need list and detail page
then you need to use React-Router
useParam to get id from url.
use that id as key in useQuery
this key is available on key = “queryKey” in caller fn.
need for useQueries hook
if the number of useQuery are changing from render to render, it violates rules of hook.
there fore we have useQueries hook, so that we can execute dynamic number of queries
implementing pagination
we can use key = keepPreviousData : true
so that we do not see loading indicator
also don’t forget to use, pageParam, setPageParam
infinite query
- useInfiniteQuery instead of useQuery
- hasNextPage
- fetchNextPage
instead of data.data, you will get data.pages
how to save a get request after mutation
by setting the data returned from mutation into the queryClient.setQueryData
optimistic update uses three keys
onMutate
onError,
onSettled