Replies: 3 comments 4 replies
-
I think with the custom hook you are pretty much almost there yet. You can also abstract that further of course:
this is then similar to the abstraction that you have on the query level |
Beta Was this translation helpful? Give feedback.
-
I think maybe all this could be solved if it was possible to communicate some sort of context between the Currently the definition of the default mutation function is: export declare type MutationFunction<TData = unknown, TVariables = unknown> = (variables: TVariables) => Promise<TData> Where export declare type MutationFunction<TData = unknown, TVariables = unknown, TContext = unknown> = (variables: TVariables, context: TContext) => Promise<TData> Which could be used for example like this: const mutation = useMutation({
context: { method: 'POST', url: '/api/blog-post' }
// onMutate, ...
}) Probably should be called something other than |
Beta Was this translation helpful? Give feedback.
-
Hi. I've got the exact same problem as you. Have you figured out something better? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've started to move API-data away from Redux and instead fetch via
react-query
.For queries this process is super easy, thanks to the possibility of providing a default query function. We have created the convention that the query key is either a string (url of endpoint), or an array with url of endpoint being the first item, and query parameters being the second item. Since I know the
baseUrl
when I create the QueryClient, I can just use this simplecreateQueryFn
to create a default fetch function:Creating shared query hooks for each endpoint is then just a matter of doing this:
For mutations on the other hand, I'm very unsure how to make things equally easy. Here's my first mutation hook, with no shared functionality:
Doing all of this for every mutation we currently have "in" Redux, will be an giant pain. Are there any good examples on how to share "fetch functionality" for mutations when using
react-query
?I looked into the
mutationFn
one can provide todefaultOptions.mutations
, which probably could've been perfect, but it only receives thevariables
you pass when callingmutate
. So there's no way to decide which endpoint to target, for example, unless you also pass that in as part of the "variables", but I'd really like to avoid that.And since the fetch requires the
baseUrl
, which is only available via the Redux store (since it can't be "locked in" when theQueryClient
is created, like I can with the defaultqueryFn
, I can't just pull out simple helper functions either.My goal is that a component just needs to call e.g.
usePutSomething(id)
, and get back a mutate function to call with only the data to put.Best solution I can come up with so far is to create a custom
useMutationFetch
hook, or something like that, which gets thebaseUrl
from the Redux store, and returns a wrapped fetch function of some sort, which is then used within theuseMutation
hook. Something like this:Much better, but still not sure I like it...
Either way, hope someone has some good ideas from their own projects here. Feel like the documentation is very lacking in guidance on this topic of how to prevent duplication related to queries and mutations. For queries there is a little bit thanks to the documentation of the default query function, but other than that, it doesn't really seem to be anything, at least of what I've found so far. The current examples in this repo all seem to define everything more or less inline for each
useQuery
/useMutation
hook, and that's not really doable in a larger application, in my opinion. Keeping keys and fetch behaviour consistent across the app would become difficult I feel. 😕Beta Was this translation helpful? Give feedback.
All reactions