zodios-react icon indicating copy to clipboard operation
zodios-react copied to clipboard

Allow passing params and queries (ZodiosRequestOptions) to mutate()

Open lnogol opened this issue 2 years ago • 6 comments

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

lnogol avatar Feb 18 '23 11:02 lnogol

This would be important. A common use case:

  1. you fetch an array of data
  2. you build a list from the array
  3. 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.

robotkutya avatar Mar 29 '23 19:03 robotkutya

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: '... } });  
} 

hanayashiki avatar Apr 05 '23 07:04 hanayashiki

This will be available on v11 as it's a breaking change

ecyrbe avatar Apr 05 '23 09:04 ecyrbe

Hey any updates on this?

markkkkas avatar Jun 02 '23 18:06 markkkkas

I'm waiting for tanstack query v5 before releasing v11, else this would be Bad to release another breaking change soon after.

ecyrbe avatar Jun 02 '23 19:06 ecyrbe

https://github.com/TanStack/query/releases/tag/v5.0.0-rc.1 just came out, would be really nice to have this feature 😊

briandiephuis avatar Sep 25 '23 20:09 briandiephuis