Skip to content

Commit 637b0ca

Browse files
authored
Add info about preferCacheValue for lazy queries (#1924)
1 parent c2ba615 commit 637b0ca

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ type UseLazyQueryOptions = {
490490
selectFromResult?: (result: UseQueryStateDefaultResult) => any
491491
}
492492

493-
type UseLazyQueryTrigger<T> = (arg: any) => Promise<
493+
type UseLazyQueryTrigger<T> = (arg: any, preferCacheValue?: boolean) => Promise<
494494
QueryResultSelectorResult
495495
> & {
496496
arg: unknown // Whatever argument was provided to the query
@@ -559,7 +559,10 @@ type UseLazyQuerySubscriptionOptions = {
559559
refetchOnFocus?: boolean
560560
}
561561

562-
type UseLazyQuerySubscriptionTrigger = (arg: any) => void
562+
type UseLazyQuerySubscriptionTrigger = (
563+
arg: any,
564+
preferCacheValue?: boolean
565+
) => void
563566
```
564567
565568
- **Parameters**

docs/rtk-query/usage/queries.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ There are 5 query-related hooks:
109109
3. [`useQueryState`](../api/created-api/hooks.mdx#usequerystate)
110110
- Returns the query state and accepts `skip` and `selectFromResult`. Reads the request status and cached data from the Redux store.
111111
4. [`useLazyQuery`](../api/created-api/hooks.mdx#uselazyquery)
112-
- Returns a tuple with a `fetch` function, the query result, and last promise info. Similar to `useQuery`, but with manual control over when the data fetching occurs.
112+
- Returns a tuple with a `trigger` function, the query result, and last promise info. Similar to `useQuery`, but with manual control over when the data fetching occurs. **Note: the `trigger` function takes a second argument of `preferCacheValue?: boolean` in the event you want to skip making a request if cached data already exists.**
113113
5. [`useLazyQuerySubscription`](../api/created-api/hooks.mdx#uselazyquerysubscription)
114-
- Returns a tuple with a `fetch` function, and last promise info. Similar to `useQuerySubscription`, but with manual control over when the data fetching occurs.
114+
- Returns a tuple with a `trigger` function, and last promise info. Similar to `useQuerySubscription`, but with manual control over when the data fetching occurs. **Note: the `trigger` function takes a second argument of `preferCacheValue?: boolean` in the event you want to skip making a request if cached data already exists.**
115115

116116
In practice, the standard `useQuery`-based hooks such as `useGetPostQuery` will be the primary hooks used in your application, but the other hooks are available for specific use cases.
117117

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export type UseLazyQueryLastPromiseInfo<
185185
*
186186
* #### Note
187187
*
188-
* When the trigger function returned from a LazyQuery, it always initiates a new request to the server even if there is cached data. Set `preferCacheValue`(the second argument to the function) as true if you want it to use cache.
188+
* When the trigger function returned from a LazyQuery, it always initiates a new request to the server even if there is cached data. Set `preferCacheValue`(the second argument to the function) as `true` if you want it to immediately return a cached value if one exists.
189189
*/
190190
export type UseLazyQuery<D extends QueryDefinition<any, any, any, any>> = <
191191
R = UseQueryStateDefaultResult<D>

0 commit comments

Comments
 (0)