-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
onMutate
never actually runs synchronously
#8724
Comments
We guarantee that
So, no, I don’t think |
Sure, the side-effects of onMutate could be asynchronous and be awaited.
But my expectation is that the handler **fired** synchronously, esp since
it's used for optimistic updates.
For example, onMutate is currently unusable for optimistic updates to
dndkit drag and drop state, bc the optimistic update fires asynchronously:
https://stackoverflow.com/questions/79168785/react-query-with-dnd-kit-item-goes-back-to-original-position-for-a-split-second
…On Mon, Mar 3, 2025 at 10:19 AM Dominik Dorfmeister < ***@***.***> wrote:
We guarantee that onMutate runs synchronously, because it can in itself
return a promise. We also await the result of it, it’s in your diff:
const context = await this.options.onMutate?.(variables);
So, no, I don’t think onMutate is meant to be syncronous.
—
Reply to this email directly, view it on GitHub
<#8724 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALPSUNVXD7AMHHVJZRXGZT2SRXHBAVCNFSM6AAAAABYDVBFAOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMOJUG42DEMRQGY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
[image: TkDodo]*TkDodo* left a comment (TanStack/query#8724)
<#8724 (comment)>
We guarantee that onMutate runs synchronously, because it can in itself
return a promise. We also await the result of it, it’s in your diff:
const context = await this.options.onMutate?.(variables);
So, no, I don’t think onMutate is meant to be syncronous.
—
Reply to this email directly, view it on GitHub
<#8724 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALPSUNVXD7AMHHVJZRXGZT2SRXHBAVCNFSM6AAAAABYDVBFAOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMOJUG42DEMRQGY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
okay I think the change in the PR doesn’t hurt, so feel free to file it. However, this then means that the behaviour changes depending on if you have an So I really wouldn’t rely on it being synchronous, as this would change as soon as you add |
onMutate
never actually runs synchronously because the result of#mutationCache.config.onMutate?.()
is always awaited beforehandDiff that fixes this:
(Diff generated via patch-package to patch
@tanstack/query-core@5.51.21
)The text was updated successfully, but these errors were encountered: