Skip to content

Commit 167ba5d

Browse files
authored
Merge pull request #3963 from reduxjs/rtkq-without-hooks
2 parents 70c1c5c + 61c54b2 commit 167ba5d

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

docs/rtk-query/usage/usage-without-react-hooks.mdx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,26 @@ Cache subscriptions are used to tell RTK Query that it needs to fetch data for a
2323
With React hooks, this behavior is instead handled within [`useQuery`](../api/created-api/hooks.mdx#usequery), [`useQuerySubscription`](../api/created-api/hooks.mdx#usequerysubscription), [`useLazyQuery`](../api/created-api/hooks.mdx#uselazyquery), and [`useLazyQuerySubscription`](../api/created-api/hooks.mdx#uselazyquerysubscription).
2424

2525
```ts title="Subscribing to cached data" no-transpile
26+
const promise = dispatch(api.endpoints.getPosts.initiate())
27+
const { refetch } = promise
2628
// interact with the cache in the same way as you would with a useFetch...() hook
27-
const {data, refetch, isLoading, isSuccess, /*...*/} = dispatch(api.endpoints.getPosts.initiate())
29+
const { data, isLoading, isSuccess /*...*/ } = await promise
2830
```
2931

3032
## Removing a subscription
3133

3234
Removing a cache subscription is necessary for RTK Query to identify that cached data is no longer required. This allows RTK Query to clean up and remove old cache data.
3335

34-
The result of dispatching the [`initiate`](../api/created-api/endpoints.mdx#initiate) thunk action creator of a query endpoint is an object with an `unsubscribe` property. This property is a function that when called, will remove the corresponding cache subscription.
36+
The result of dispatching the [`initiate`](../api/created-api/endpoints.mdx#initiate) thunk action creator of a query endpoint is a Promise with an `unsubscribe` property. This property is a function that when called, will remove the corresponding cache subscription.
3537

3638
With React hooks, this behavior is instead handled within [`useQuery`](../api/created-api/hooks.mdx#usequery), [`useQuerySubscription`](../api/created-api/hooks.mdx#usequerysubscription), [`useLazyQuery`](../api/created-api/hooks.mdx#uselazyquery), and [`useLazyQuerySubscription`](../api/created-api/hooks.mdx#uselazyquerysubscription).
3739

3840
```ts title="Unsubscribing from cached data" no-transpile
3941
// Adding a cache subscription
40-
const result = dispatch(api.endpoints.getPosts.initiate())
42+
const promise = dispatch(api.endpoints.getPosts.initiate())
4143

4244
// Removing the corresponding cache subscription
43-
result.unsubscribe()
45+
promise.unsubscribe()
4446
```
4547

4648
## Accessing cached data & request status
@@ -49,18 +51,35 @@ Accessing cache data and request status information can be performed using the `
4951

5052
:::caution
5153

52-
The `endpoint.select()` function creates a _new_ selector instance - it isn't the actual selector function itself!
54+
The `endpoint.select(arg)` function creates a _new_ selector instance - it isn't the actual selector function itself!
5355

5456
:::
5557

5658
With React hooks, this behaviour is instead handled within [`useQuery`](../api/created-api/hooks.mdx#usequery), [`useQueryState`](../api/created-api/hooks.mdx#usequerystate), and [`useLazyQuery`](../api/created-api/hooks.mdx#uselazyquery).
5759

5860
```ts title="Accessing cached data & request status" no-transpile
5961
const result = api.endpoints.getPosts.select()(state)
60-
const { data, status, error } = result
62+
const { data, isSuccess, isError, error } = result
6163
```
6264

63-
Note that unlike the auto-generated query hooks, derived booleans such as `isLoading`, `isFetching`, `isSuccess` are not available here. The raw `status` enum is provided instead.
65+
Note that unlike with the auto-generated hooks, there is no `isFetching` flag, and the `isLoading` flag will be true if the status is pending, regardless of if there is already data.
66+
67+
### Memoization
68+
69+
Because the `endpoint.select(arg)` function returns a new selector each time it's called, and because this instance itself is memoized, it can be desirable to memoize the creation of a selector (for example, to then use that memoized instance in another selector). This can be done with `createSelector`:
70+
71+
```ts title="Creating a memoized selector creator" no-transpile
72+
const createGetPostSelector = createSelector(
73+
(id: string) => id,
74+
(id) => api.endpoints.getPost.select(id)
75+
)
76+
77+
const selectGetPostError = createSelector(
78+
(state: RootState) => state,
79+
(state: RootState, id: string) => createGetPostSelector(id),
80+
(state, selectGetPost) => selectGetPost(state).error
81+
)
82+
```
6483

6584
## Performing mutations
6685

0 commit comments

Comments
 (0)