`withOptimisticUpdate` with custom (auth) mutations
I have the following custom mutation:
which automatically injects the clerkOrgId into the args. I'm unfortunately not able to do useAuthMutation().withOptimisticUpdate().
Can anyone who has worked on convex-helpers offer suggestions? 🙏
7 Replies
@erquhart
You can take the
.withOptimisticUpdate
method and pass it along in your wrapper; that's what happens in the real client, it's just a function tacked on there https://github.com/get-convex/convex-js/blob/533f0705af13fb790be4b58ea10735aa94ee8d7c/src/react/client.ts#L88GitHub
convex-js/src/react/client.ts at 533f0705af13fb790be4b58ea10735aa94...
TypeScript/JavaScript client library for Convex. Contribute to get-convex/convex-js development by creating an account on GitHub.
so instead of returning the useCallback result directly, tack this method on first and then return it. Or use a useMemo instead so you can modify that function object inside the hook.
Then you might enforce that it
satisfies
ReactMutation<F>
(or cast it) to convince TypeScript that this method exists
I'm using something like this ^^
I don't know what the AuthMutation type is, so it may need some tweaking
I had to add an await for TS to not complain about
Property 'withOptimisticUpdate' does not exist on type 'Promise<FunctionReturnType<Mutation>>'.ts(2339)
With this
and this front-end code:
there's no TS errors but when I run the mutation I get the following in the console: TypeError: mutation.withOptimisticUpdate is not a function
Ah, yep, updated:
beautiful, that works 🔥