Skip to content

Instantly share code, notes, and snippets.

@adrian-afergon
Created May 15, 2023 07:59
Show Gist options
  • Save adrian-afergon/3185e73109176cb595744ca356711b3e to your computer and use it in GitHub Desktop.
Save adrian-afergon/3185e73109176cb595744ca356711b3e to your computer and use it in GitHub Desktop.
import {useSWRConfig} from "swr";
interface UseDataMutation<T> {
key: string,
mutation: (data: T) => Promise<T>
}
export const useDataMutation = <T>({key, mutation}: UseDataMutation<T> ) => {
const { mutate: swrMutate } = useSWRConfig()
const mutate = async (data: T) => {
await swrMutate(key, mutation(data))
}
return {mutate}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment