Skip to content

Commit ab346b9

Browse files
authored
fixdoc: incorrect position of keepUnusedDataFor for two scenarios (#5062)
* fix: incorrect position of keepUnusedDataFor for two scenarios * fix: syntax issue * fix: add a missing import for page "Cache Behavior" * fix: resolve conflict
1 parent d2bbb8d commit ab346b9

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

docs/rtk-query/api/createApi.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ By default, this function will take the query arguments, sort object keys where
482482

483483
[summary](docblock://query/createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
484484

485+
[examples](docblock://query/createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
486+
485487
### `refetchOnMountOrArgChange`
486488

487489
[summary](docblock://query/createApi.ts?token=CreateApiOptions.refetchOnMountOrArgChange)
@@ -684,7 +686,7 @@ Overrides the api-wide definition of `keepUnusedDataFor` for this endpoint only.
684686

685687
[summary](docblock://query/createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
686688

687-
[examples](docblock://query/createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
689+
[examples](docblock://query/core/buildMiddleware/cacheCollection.ts?token=CacheCollectionQueryExtraOptions)
688690

689691
### `serializeQueryArgs`
690692

docs/rtk-query/usage/cache-behavior.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Alternatively, you can dispatch the `initiate` thunk action for an endpoint, pas
122122

123123
```tsx no-transpile title="Force refetch example"
124124
import { useDispatch } from 'react-redux'
125-
import { useGetPostsQuery } from './api'
125+
import { api, useGetPostsQuery } from './api'
126126

127127
const Component = () => {
128128
const dispatch = useDispatch()

packages/toolkit/src/query/core/buildMiddleware/cacheCollection.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ import type {
1111

1212
export type ReferenceCacheCollection = never
1313

14+
/**
15+
* @example
16+
* ```ts
17+
* // codeblock-meta title="keepUnusedDataFor example"
18+
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
19+
* interface Post {
20+
* id: number
21+
* name: string
22+
* }
23+
* type PostsResponse = Post[]
24+
*
25+
* const api = createApi({
26+
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
27+
* endpoints: (build) => ({
28+
* getPosts: build.query<PostsResponse, void>({
29+
* query: () => 'posts',
30+
* // highlight-start
31+
* keepUnusedDataFor: 5
32+
* // highlight-end
33+
* })
34+
* })
35+
* })
36+
* ```
37+
*/
1438
export type CacheCollectionQueryExtraOptions = {
1539
/**
1640
* Overrides the api-wide definition of `keepUnusedDataFor` for this endpoint only. _(This value is in seconds.)_

packages/toolkit/src/query/createApi.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ export interface CreateApiOptions<
109109
/**
110110
* Defaults to `60` _(this value is in seconds)_. This is how long RTK Query will keep your data cached for **after** the last component unsubscribes. For example, if you query an endpoint, then unmount the component, then mount another component that makes the same request within the given time frame, the most recent value will be served from the cache.
111111
*
112+
* @example
112113
* ```ts
113114
* // codeblock-meta title="keepUnusedDataFor example"
114-
*
115115
* import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
116116
* interface Post {
117117
* id: number
@@ -123,12 +123,12 @@ export interface CreateApiOptions<
123123
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
124124
* endpoints: (build) => ({
125125
* getPosts: build.query<PostsResponse, void>({
126-
* query: () => 'posts',
127-
* // highlight-start
128-
* keepUnusedDataFor: 5
129-
* // highlight-end
126+
* query: () => 'posts'
130127
* })
131-
* })
128+
* }),
129+
* // highlight-start
130+
* keepUnusedDataFor: 5
131+
* // highlight-end
132132
* })
133133
* ```
134134
*/

website/docusaurus.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const config: Config = {
3636
'query/endpointDefinitions.ts',
3737
'query/react/index.ts',
3838
'query/react/ApiProvider.tsx',
39+
'query/core/buildMiddleware/cacheCollection.ts',
3940
],
4041
},
4142
},

0 commit comments

Comments
 (0)