You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
requestId:string// A string generated by RTK Query
82
-
abort: () =>void// A method to cancel the mutation promise
83
-
unwrap: () =>Promise<T> // A method to unwrap the mutation call and provide the raw response/error
84
-
unsubscribe: () =>void// A method to manually unsubscribe from the mutation call
85
-
}
86
-
```
73
+
### Performing Mutations with React Hooks
74
+
75
+
#### Frequently Used Mutation Hook Return Values
76
+
77
+
The `useMutation` hook returns a tuple containing a `mutation trigger` function, as well as an object containing properties about the `mutation result`.
78
+
79
+
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.
80
+
81
+
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.
82
+
83
+
Below are some of the most frequently used properties on the `mutation result` object. Refer to [`useMutation`](../../api/rtk-query/created-api/hooks.mdx#usemutation) for an extensive list of all returned properties.
84
+
85
+
-`data` - The returned result if present.
86
+
-`error` - The error result if present.
87
+
-`isUninitialized` - When true, indicates that the mutation has not been fired yet.
88
+
-`isLoading` - When true, indicates that the mutation has been fired and is awaiting a response.
89
+
-`isSuccess` - When true, indicates that the last mutation fired has data from a successful request.
90
+
-`isError` - When true, indicates that the last mutation fired resulted in an error state.
91
+
92
+
:::note
93
+
94
+
With RTK Query, a mutation does contain a semantic distinction between 'loading' and 'fetching' in the way that a [query does](./queries.mdx#frequently-used-query-hook-return-values). 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'.
95
+
96
+
:::
97
+
98
+
#### Example
87
99
88
100
This 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.
Copy file name to clipboardExpand all lines: docs/usage/rtk-query/queries.mdx
+15-21Lines changed: 15 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,7 @@ const api = createApi({
73
73
})
74
74
```
75
75
76
-
### Performing queries with React Hooks
76
+
### Performing Queries with React Hooks
77
77
78
78
If you're using React Hooks, RTK Query does a few additional things for you. The primary benefit is that you get a render-optimized hook that allows you to have 'background fetching' as well as [derived booleans](#query-hook-return-types) for convenience.
79
79
@@ -105,26 +105,20 @@ There are 5 query-related hooks:
105
105
106
106
> All `refetch`-related options will override the defaults you may have set in [createApi](../../api/rtk-query/createApi)
107
107
108
-
#### Query Hook Return Types
109
-
110
-
```ts title="All returned elements" no-transpile
111
-
// Base query state
112
-
originalArgs?:unknown; // Arguments passed to the query
113
-
data?:unknown; // Returned result if present
114
-
error?:unknown; // Error result if present
115
-
requestId?:string; // A string generated by RTK Query
116
-
endpointName?:string; // The name of the given endpoint for the query
117
-
startedTimeStamp?:number; // Timestamp for when the query was initiated
118
-
fulfilledTimeStamp?:number; // Timestamp for when the query was completed
119
-
120
-
isUninitialized: false; // Query has not started yet.
121
-
isLoading: false; // Query is currently loading for the first time. No data yet.
122
-
isFetching: false; // Query is currently fetching, but might have data from an earlier request.
123
-
isSuccess: false; // Query has data from a successful load.
124
-
isError: false; // Query is currently in "error" state.
125
-
126
-
refetch: () =>void; // A function to force refetch the query
127
-
```
108
+
#### Frequently Used Query Hook Return Values
109
+
110
+
The query hook returns an object containing properties such as the latest `data` for the query request, as well as status booleans for the current request lifecycle state. Below are some of the most frequently used properties. Refer to [`useQuery`](../../api/rtk-query/created-api/hooks.mdx#usequery) for an extensive list of all returned properties.
111
+
112
+
-`data` - The returned result if present.
113
+
-`error` - The error result if present.
114
+
-`isUninitialized` - When true, indicates that the query has not started yet.
115
+
-`isLoading` - When true, indicates that the query is currently loading for the first time, and has no data yet. This will be `true` for the first request fired off, but _not_ for subsequent requests.
116
+
-`isFetching` - When true, indicates that the query is currently fetching, but might have data from an earlier request. This will be `true` for both the first request fired off, as well as subsequent requests.
117
+
-`isSuccess` - When true, indicates that the query has data from a successful request.
118
+
-`isError` - When true, indicates that the query is in an `error` state.
119
+
-`refetch` - A function to force refetch the query
0 commit comments