You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hi guys, I'm using swr for global state management. And in my situation, I want the fetcher to request API under certain conditions, which is stored in its corresponding data, see below:
functionuseTaskData(){constcacheKey='api_generateStatus';const{
cache,}=useSWRConfig();const{
data,
mutate,}=useSWR(cacheKey,asyncfunction(){// here I want to read the cache. But the `currentData` got from cache, is not what I put savedconstcurrentData=cache.get(cacheKey)?.data||null;if(currentData&¤tData.task_id){// request api}else{returnnull;}},{fallbackData: null,refreshInterval(data){if(data&&data.task_id){// if we have a task_id, then start auto refresh intervalreturn1000;}return0;},});return{
data,
mutate,};}
and in some other components, I will call mutate to update the data, triggering the auto refresh interval.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
hi guys, I'm using
swr
for global state management. And in my situation, I want thefetcher
to request API under certain conditions, which is stored in its corresponding data, see below:and in some other components, I will call
mutate
to update the data, triggering the auto refresh interval.my questions are:
fetcher
function?cache.get(someKey)
is not the same as wereturn
infetcher
fetcher
function (this is similar to the 1st question)any suggestions are appreciated, thx in advance.
Beta Was this translation helpful? Give feedback.
All reactions