Skip to content

Commit

Permalink
Update docs to avoid circular type
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Dec 5, 2023
1 parent cf29e62 commit 3ffa3a6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 10 additions & 5 deletions docs/rtk-query/usage/persistence-and-rehydration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,27 @@ import { REHYDRATE } from 'redux-persist'

type RootState = any // normally inferred from state

function isHydrateAction(action: Action): action is PayloadAction<RootState> {
function isHydrateAction(action: Action): action is Action<typeof REHYDRATE> & {
key: string
payload: RootState
err: unknown
} {
return action.type === REHYDRATE
}

export const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
// highlight-start
extractRehydrationInfo(action, { reducerPath }) {
// to prevent circular type issues, the return type needs to be annotated as any
extractRehydrationInfo(action, { reducerPath }): any {
if (isHydrateAction(action)) {
if ((action as any).key === 'key used with redux-persist') {
// when persisting the api reducer
// when persisting the api reducer
if (action.key === 'key used with redux-persist') {
return action.payload
}

// When persisting the root reducer
return action.payload[reducerPath]
return action.payload[api.reducerPath]
}
},
// highlight-end
Expand Down
10 changes: 7 additions & 3 deletions packages/toolkit/src/query/createApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,18 @@ export interface CreateApiOptions<
*
* type RootState = any; // normally inferred from state
*
* function isHydrateAction(action: Action): action is PayloadAction<RootState> {
* return action.type === HYDRATE
* function isHydrateAction(action: Action): action is Action<typeof REHYDRATE> & {
* key: string
* payload: RootState
* err: unknown
* } {
* return action.type === REHYDRATE
* }
*
* export const api = createApi({
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
* // highlight-start
* extractRehydrationInfo(action, { reducerPath }) {
* extractRehydrationInfo(action, { reducerPath }): any {
* if (isHydrateAction(action)) {
* return action.payload[reducerPath]
* }
Expand Down

0 comments on commit 3ffa3a6

Please sign in to comment.