#
MutationsUnlike useQuery
, useMutation
returns a tuple. The first item in the tuple is the trigger
function and the second element contains an object with status
, error
, and data
.
Unlike the useQuery
hook, the useMutation
hook doesn't execute automatically. To run a mutation you have to call the trigger function returned as the first tuple value from the hook.
See useMutation
for the hook signature and additional details.
- TypeScript
- JavaScript
info
Notice the onQueryStarted
method? Be sure to check out how it can be used for optimistic updates
#
Performing Mutations with React Hooks#
Frequently Used Mutation Hook Return ValuesThe useMutation
hook returns a tuple containing a mutation trigger
function, as well as an object containing properties about the mutation result
.
The mutation trigger
is a function that when called, will fire off the mutation request for that endpoint. Calling the mutation trigger
returns a promise with an unwrap
property, which can be called to unwrap the mutation call and provide the raw response/error. This can be useful if you wish to determine whether the mutation succeeds/fails inline at the call-site.
The mutation result
is an object containing properties such as the latest data
for the mutation request, as well as status booleans for the current request lifecycle state.
Below are some of the most frequently used properties on the mutation result
object. Refer to useMutation
for an extensive list of all returned properties.
data
- The returned result if present.error
- The error result if present.isUninitialized
- When true, indicates that the mutation has not been fired yet.isLoading
- When true, indicates that the mutation has been fired and is awaiting a response.isSuccess
- When true, indicates that the last mutation fired has data from a successful request.isError
- When true, indicates that the last mutation fired resulted in an error state.
note
With RTK Query, a mutation does contain a semantic distinction between 'loading' and 'fetching' in the way that a query does. For a mutation, subsequent calls are not assumed to be necessarily related, so a mutation is either 'loading' or 'not loading', with no concept of 're-fetching'.
#
ExampleThis is a modified version of the complete example you can see at the bottom of the page to highlight the updatePost
mutation. In this scenario, a post is fetched with useQuery
, and then a EditablePostName
component is rendered that allows us to edit the name of the post.
#
Advanced mutations with revalidationIn the real world, it's very common that a developer would want to resync their local data cache with the server after performing a mutation (aka "revalidation"). RTK Query takes a more centralized approach to this and requires you to configure the invalidation behavior in your API service definition. See Advanced Invalidation with abstract tag IDs for details on advanced invalidation handling with RTK Query.
#
Example - Commented Posts ServiceThis is an example of a CRUD service for Posts. This implements the Selectively invalidating lists strategy and will most likely serve as a good foundation for real applications.
- TypeScript
- JavaScript