Skip to content

Commit 4b629cb

Browse files
committed
- restructure hook return values on query & mutation usage pages
1 parent 405cd43 commit 4b629cb

File tree

3 files changed

+48
-39
lines changed

3 files changed

+48
-39
lines changed

docs/api/rtk-query/created-api/hooks.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,10 @@ type UseMutationResult<T> = {
325325
fulfilledTimestamp?: number // Timestamp for when the mutation was completed
326326

327327
// Derived request status booleans
328-
isError: boolean // Mutation is currently in an "error" state
328+
isUninitialized: boolean // Mutation has not been fired yet
329329
isLoading: boolean // Mutation has been fired and is awaiting a response
330330
isSuccess: boolean // Mutation has data from a successful call
331-
isUninitialized: boolean // Mutation has not been fired yet
331+
isError: boolean // Mutation is currently in an "error" state
332332
startedTimeStamp?: number // Timestamp for when the latest mutation was initiated
333333
}
334334
```

docs/usage/rtk-query/mutations.mdx

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,32 @@ const api = createApi({
7070
Notice the `onQueryStarted` method? Be sure to check out how it can be used for [optimistic updates](./optimistic-updates)
7171
:::
7272

73-
### Basic Mutation with React Hooks
74-
75-
#### Mutation Hook Trigger Type
76-
77-
```ts title="Mutation hook type" no-transpile
78-
type UseMutationTrigger<T> = (
79-
arg: any
80-
) => Promise<{ data: T } | { error: BaseQueryError | SerializedError }> & {
81-
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
8799

88100
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.
89101

@@ -93,11 +105,12 @@ export const PostDetail = () => {
93105

94106
const { data: post } = useGetPostQuery(id)
95107

108+
// highlight-start
96109
const [
97-
// highlight-next-line
98110
updatePost, // This is the mutation trigger
99-
{ isLoading: isUpdating }, // You can use the `isLoading` flag, or do custom logic with `status`
111+
{ isLoading: isUpdating }, // This is the destructured mutation result
100112
] = useUpdatePostMutation()
113+
// highlight-end
101114

102115
return (
103116
<Box p={4}>
@@ -115,7 +128,9 @@ export const PostDetail = () => {
115128
// highlight-end
116129
)
117130
}}
131+
// highlight-start
118132
isLoading={isUpdating}
133+
// highlight-end
119134
/>
120135
</Box>
121136
)

docs/usage/rtk-query/queries.mdx

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const api = createApi({
7373
})
7474
```
7575

76-
### Performing queries with React Hooks
76+
### Performing Queries with React Hooks
7777

7878
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.
7979

@@ -105,26 +105,20 @@ There are 5 query-related hooks:
105105

106106
> All `refetch`-related options will override the defaults you may have set in [createApi](../../api/rtk-query/createApi)
107107
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
120+
121+
#### Example
128122

129123
Here is an example of a `PostDetail` component:
130124

0 commit comments

Comments
 (0)