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
Copy file name to clipboardExpand all lines: docs/rtk-query/usage/usage-without-react-hooks.mdx
+26-7Lines changed: 26 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -23,24 +23,26 @@ Cache subscriptions are used to tell RTK Query that it needs to fetch data for a
23
23
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).
24
24
25
25
```ts title="Subscribing to cached data" no-transpile
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.
33
35
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.
35
37
36
38
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).
37
39
38
40
```ts title="Unsubscribing from cached data" no-transpile
@@ -49,18 +51,35 @@ Accessing cache data and request status information can be performed using the `
49
51
50
52
:::caution
51
53
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!
53
55
54
56
:::
55
57
56
58
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).
57
59
58
60
```ts title="Accessing cached data & request status" no-transpile
59
61
const result =api.endpoints.getPosts.select()(state)
60
-
const { data, status, error } =result
62
+
const { data, isSuccess, isError, error } =result
61
63
```
62
64
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
0 commit comments