Replies: 1 comment
-
|
Until this gets built in, you can achieve this today with a global <SWRConfig
value={{
revalidateOnMutate: false, // this option does not exist yet, but...
}}
>What you can do right now is wrap import useSWRMutation from "swr/mutation";
import type { SWRMutationConfiguration } from "swr/mutation";
export function useMutation<T, E = any>(
key: string,
fetcher: (key: string, options: { arg: any }) => Promise<T>,
options?: SWRMutationConfiguration<T, E> & { revalidate?: boolean }
) {
return useSWRMutation(key, fetcher, {
revalidate: false, // default to no revalidation
...options, // allow overriding per-call
});
}Then use it everywhere instead of the raw hook: const { trigger } = useMutation("/api/items", updateItem);
// No revalidation by default
const { trigger } = useMutation("/api/items", updateItem, { revalidate: true });
// Opt-in when you need itThis is especially useful when your API returns the updated resource and you use That said, having a first-class |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
This would set
revalidate: falsefor all bound mutations. Currently it istrueby default. https://swr.vercel.app/docs/mutation.en-US#parametersThis would be useful when the api returns a result where revalidation is unnecessary or the developer knows the result of a successful response.
Beta Was this translation helpful? Give feedback.
All reactions