mutationOptions
ThemutationOptions helper provides type-safe mutation options with proper type inference. It’s a lightweight identity function that helps TypeScript infer types correctly and provides better autocomplete for mutations.
Import
Signature
Type Parameters
type
default:"unknown"
The type of data returned by the mutation function
type
default:"DefaultError"
The type of error that can be thrown by the mutation function
type
default:"void"
The type of variables passed to the mutation function
type
default:"unknown"
The type of context returned by the onMutate callback
Parameters
UseMutationOptions
required
Mutation configuration options. Accepts all options that
useMutation accepts.Returns
Returns the same options object that was passed in, with enhanced type information for better TypeScript inference.Examples
Basic Usage
Reusable Mutation Definitions
With Mutation Key
With Optimistic Updates
With Type Inference
Factory Pattern
Notes
mutationOptionsis an identity function that returns the exact object passed to it.- Its primary purpose is to improve TypeScript type inference and provide better autocomplete.
- It’s particularly useful when you want to share mutation configurations across multiple components or files.
- Unlike
queryOptions, themutationKeyis optional for mutations. - This pattern is recommended for organizing and reusing mutation configurations in larger applications.
- The helper supports all options available in
useMutation, including callbacks likeonSuccess,onError, andonMutate.