Feature request: useMutation should return 'createMutation' and 'getMutution' functions to allow for concurrent mutations of the same type. #4613
Replies: 2 comments 1 reply
-
I think examples 1 or 3 are perfectly fine. I would always prefer to keep mutations close to where they are used. What would be the point of having a
the point about testing might be real, and its a trade-off for sure. We use |
Beta Was this translation helpful? Give feedback.
-
Any update on this? useMutation forces you to place the mutation inside the component that performs the action, and prevents from instead placing this call in the parent component that renders the list. What if I have 2 visually equal elements but should perform slightly different things when interacted with? I would like to define the logic in the parent component. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This question relates to several existing issues/discussions, I'm hoping to do a good job here of the explaining the use case.
Problem statement
In some scenarios I may want to have a per-item instance of a useMutation call. For example, I may have an array of items which I display as list, each with their own delete button. I want to track the state of individual mutation calls, and I don't want to do this by putting a
useMutation
into the specific component.Suggested solution
The
useMutation
also returns functions of the shape:Related Issues/Discussions
#2304 - This issue is more the converse - how individual
useMutation
calls have distinct results, while alluseQuery
have the same result.useIsMutating
appears to solve the issue, though I could see the need for a more flexible solution.#4009 - Looks like the same request. Suggested solution doesn't appear particularly appropriate - although I have a hacky ref solution in that line, that does work.
Details
All examples can be found here:
dwjohnston/query-play@6d7b912
Code sandbox
Example 1 - Naive example
I have a 'like' button. When a user clicks the button it should show a loading cursor, until finished, and then show success/error.
This is absolutely fine. But: I don't want to call
useMutation
directly within such a button.Example 2 - Removing
useMutation
from our special like button.We can refactor the button's props to look like this, which is more inline with what've what we've want - something state management/context agnostic.
But how do we pass in those props?
This doesn't suffice - a click on a single button will show loading buttons for all buttons.
Example 3 - Use a wrapper/container component.
This is gives us a per-item useMutation result - but it doesn't really solve the problem - what if we wanted to export a component that used this component?
Example 4 - Hacky ref solution.
This does seem to work:
However, it makes uncomfortable.
What I would like to see
I would like to be able to use some code like:
That is - the ability to configure a
useMutation
hook with the same parameters, but keep track of individual calls - individual calls being differentiated by a string id.Performance Considerations
I will mention that there are possible performance considerations here.
As opposed to doing either the Example 1 or Example 3 approach - it's possible that the solution I propose would give the scenario where a button click on a single button would cause rerenders up at the root (where the useMutation is), meaning that a single button click causes all the other buttons to also rerender.
Beta Was this translation helpful? Give feedback.
All reactions