How to create mutation key based on mutation params #7160
Replies: 3 comments 6 replies
-
|
a mutations doesn't really need a key. what are you using the key for ? |
Beta Was this translation helpful? Give feedback.
-
|
I second this. Frankly, I don't understand @TkDodo comment:
If a mutation doesn't need a key, why does In practice, the mutation key is critical for the I would suggest changing |
Beta Was this translation helpful? Give feedback.
-
|
I would not make the For a single active download, something like this is enough: const [downloadingId, setDownloadingId] = useState<string | null>(null)
const downloadMutation = useMutation({
mutationKey: ["download-document"],
mutationFn: async (documentId: string) => {
return downloadDocument(documentId)
},
onMutate: (documentId) => {
setDownloadingId(documentId)
},
onSettled: () => {
setDownloadingId(null)
},
})
return documents.map((doc) => (
<button
key={doc.id}
disabled={downloadingId === doc.id}
onClick={() => downloadMutation.mutate(doc.id)}
>
{downloadingId === doc.id ? "Downloading..." : "Download"}
</button>
))If multiple downloads can run at the same time, use a So the short version is: keep the mutation key stable, pass the document id to |
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.
-
I have a list of files that will be downloaded on click
In order to download it a mutation is triggered
If I use isLoading param of that mutation is spins on all files of the list.
I want to add
params.idas query key, but I am outside of scope for that.Is there any workaround?
Beta Was this translation helpful? Give feedback.
All reactions