-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Mutate with custom updater when no useSWR seems to have wrong state #832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@AyWa if you display the button and DisplayData all the time, and change the last argument of
is this what you expected? |
No this is not what I want to see. Sorry if I didnt explain well.
What I expect is:
Does the flow is clear now? Let me know If my explanation is correct |
I think the reason is that the when mutate is done, the revalidation is started, but the useSWR hook is not been called yet (DisplayData is not mounted). you might want to find a way to keep your hook there before displaying. like <SWRConfig>
<DisplayData visible={display} />
{!display && <button /> // for mutation const DisplayData = ({visible}) => {
const {data} = useSWR(...)
return visible && <div>....</div>
} |
I think this is a bug which was introduced by #735 Lines 445 to 463 in 7858407
we ignored a case like this // case 4:
// req------------>res
// mutate------>end |
Maybe we could reset the mutation time stamp to undefined if there is no updater in CACHE_REVALIDATORS ? const updaters = CACHE_REVALIDATORS[key]
if (updaters) {
const promises = []
for (let i = 0; i < updaters.length; ++i) {
promises.push(
updaters[i](!!shouldRevalidate, data, error, undefined, i > 0)
)
}
// return new updated value
return Promise.all(promises).then(() => {
if (error) throw error
return cache.get(key)
})
} else {
MUTATION_TS[key] = undefined
MUTATION_END_TS[key] = undefined
} |
We can't ignore the revalidation result if it's this case (mutation ended before the revalidation starts):
It sounds OK to keep the request? And it's still not clear to me why it's related to #735... I'll check which case causes that 🤔 |
Commented here: #833, it was indeed a bug! |
Thanks a lot for the fast investigation and fix !! You Rock 🥳 |
Bug report
Description / Observed Behavior
Simple case is:
key
with a custom updaterkey
-> the data that I see is not correct.
Expected Behavior
Expected behavior are:
useSWR
is done on the same key, we should expect correct data -> request is correctly done, but data does not seems to be use / dropRepo Steps / Code Example
if you run this example: click on the button you will see that request is done -> but only the mutate data is use.
https://codesandbox.io/s/clever-knuth-2z40o?file=/src/App.js:1128-1161
Additional Context
0.3.9
The text was updated successfully, but these errors were encountered: