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
I am learning this library for a new project, and I am trying to understand a complete story with mutations, using optimistic updates and revalidation. I am having some doubts about the pattern I'm using.
Here's an illustrative example of a mutation I'm using:
constupdateMyDetails=(details: Details)=>{returnmutate('my-details',()=>client.request(/* just some request here, doesn't matter what */),{optimisticData: {{ ...details}},rollbackOnError: true,populateCache: (result,_currentData)=>result,revalidate: false,})}
So this appears to work as expected. I load the details with useSWR using cache-key 'my-details', and then mutate against the same key.
Now, what I actually need to consider is this...
These "details" are a subset of some larger entity that may already be cached, so for example a "User" record contains this "Details" record and a load of other stuff. I now want to revalidate the User record, because otherwise it will retain the stale data from before this mutation. So I have two keys to invalidate, this mutation for the details, and secondly the user.
My thinking therefore is I need to explicitly revalidate this "user" cache, so I must use another mutate call to invalidate that key?
mutate(key=>key==='user')
And this is where my doubts creep in.
I change my code above to this:
constupdateMyDetails=async(details: Details)=>{constresult=awaitmutate('my-details',()=>client.request(/* just some request here, doesn't matter what */),{optimisticData: {{ ...details}},rollbackOnError: true,populateCache: (result,currentData)=>result,revalidate: false,})mutate(key=>key==='user')returnresult}
Here I explicitly await the mutation request to complete, and then invoke mutate again to invalidate the other cached value.
Is this correct?
Does this not now block the optimistic update? Or if it does not, might it revalidate the user cache before the mutation query completes?
Do I even need to worry about this, would the cached user details be revalidated when I return to the full user screen anyway?
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.
-
I am learning this library for a new project, and I am trying to understand a complete story with mutations, using optimistic updates and revalidation. I am having some doubts about the pattern I'm using.
Here's an illustrative example of a mutation I'm using:
So this appears to work as expected. I load the details with useSWR using cache-key 'my-details', and then mutate against the same key.
Now, what I actually need to consider is this...
These "details" are a subset of some larger entity that may already be cached, so for example a "User" record contains this "Details" record and a load of other stuff. I now want to revalidate the User record, because otherwise it will retain the stale data from before this mutation. So I have two keys to invalidate, this mutation for the details, and secondly the user.
My thinking therefore is I need to explicitly revalidate this "user" cache, so I must use another mutate call to invalidate that key?
And this is where my doubts creep in.
I change my code above to this:
Here I explicitly await the mutation request to complete, and then invoke mutate again to invalidate the other cached value.
Is this correct?
Does this not now block the optimistic update? Or if it does not, might it revalidate the user cache before the mutation query completes?
Do I even need to worry about this, would the cached user details be revalidated when I return to the full user screen anyway?
Or am I thinking about this all wrong?!
Beta Was this translation helpful? Give feedback.
All reactions