Allow passing params and queries (ZodiosRequestOptions) to mutate()
currently hooks.use[Alias] (and other mutation hooks) require passing params and queries to the hook call
it would be nice to be able to pass that to .mutate() instead, for cases where a param or query is not held in state / not known during the hook call
This would be important. A common use case:
- you fetch an array of data
- you build a list from the array
- each list item has an event handler that you have to pass a mutate onto
If your hooks.use[Alias] function is dependent on the fetched data, you are in trouble, because you can't instantiate the const { mutate } = hooks.use[Alias] mutation inside the event handler, without getting an error "Hooks can only be called inside the body of a function component."
With vanilla react-query, you can do this by providing a mutationFn for the useMutation hook that has the params coming from the data, and you can call the mutation function inside event handlers.
Any updates on this?
Without breaking the current API, I might suggest passing optional queries and path params to the second parameter of mutate/mutateAsync, and make it required if any of those parameters are not passed in the use hook.
const { mutateAsync } = hooks.usePost('/book/:id');
const onClick = () => {
await mutateAsync({ ...body }, { queries: { action: '...' }, params: { id: '... } });
}
This will be available on v11 as it's a breaking change
Hey any updates on this?
I'm waiting for tanstack query v5 before releasing v11, else this would be Bad to release another breaking change soon after.
https://github.com/TanStack/query/releases/tag/v5.0.0-rc.1 just came out, would be really nice to have this feature 😊