React Query
Project Setup
1) Wrap entire app.js with QueryProvider
2) New create instance of QueryClient and add it to client as shown in video
#LEARN BASIC QUERY
1)
a) Import useQuery and axios
b) Use useQuery with a key which needs to be unique and return the result
c) We get lots of value upon a execution of useQuery so we destructure and get only the two value mentioned. Please note that we have to atleast get two value
d) While loading use isLoading flag and for fetching data use data flag
If we had use useEffect and state than the code would had been much more so first advantage of this.
Common pattern also use is that using fetcher function as seperate function.Please note use the function inside RQSuperHero... ! As an improvement its better if you put fetchSuperHeros function inside RQSuperHeroesPage component with usecallback instead of outside component.
but I think this is done to help decouple the two functions. If you
moved the fetchSuperHeroes to a separate file, you could more easily
make changes to the two without stepping on the toes of each function.
Traditionally Handle Error
Handling Error using react-qery
a) Use isError to check error is there
b) Use error to get the error data
React Query Dev Tools
Add it in app.js
Query cache
Query cache allows to show prev data instaed of showing loading again. while it refetch in backgrund and show when data is available for better ui experience
How it works
it first check if cache data is availbae for particular query key ,if yes than directly show data and only after it fectch data
isFetching is used to fetch data in background .
5 mins is default value of query cache before it again do isLoading which is a good value
However,to change do below step
Use cacheTime to change default value (in ms)
Stale Time
Stale time is something which means that react-query wont even fetch latest data in background for that time coz of which isFetching remains false if you visit that page during that time.
This is usually done for stuff if you have data which don't get update often.
During that stale time in dev tool the fresh flag remains open that data is fresh and dont to be refetch
After staleTime is over query will be change to stale.
It will again show isFetching as true and than false
Default stale time is 0 sec.
Refetch Defaults
1) Refetch on Mount
Data is fetch when compoenent is mount and is true by default (
2) Refetch on window focus
This means that when we again go back to that website the data automatically is refetch. This is default to true
The above logic is good if u want your UI to be in sync with your database.
Important Point: Data is always fetch so plz note
Polling Data:
Polling data at regular interval irresective of reftchonMount or Windowfocus
Background refetching is turn of by default
However, to enable
UseQuery for click
Fetch data on browser event
1) when enabled is false data is not fetch automatically
2) add refetch to manually fetch data
When we click on button data fetching takes place. if u click again isLoading is false ..as told prev
Sucesss and Error Callbacks
For error the data is fetch 3 time before error msg is show
WE can also pass on the data as well as error message like this
This is pretty powerful as we can perform various callback based on success result or err message
b
Comments
Post a Comment