Skip to content

Commit 5eb3680

Browse files
authored
Merge pull request #3948 from reduxjs/extractrehydrationinfo-circular
2 parents cf29e62 + 92c529c commit 5eb3680

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

docs/rtk-query/usage/persistence-and-rehydration.mdx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,33 @@ box with the `autoMergeLevel1` or `autoMergeLevel2` [state reconcilers](https://
3737
when persisting the root reducer, or with the `autoMergeLevel1` reconciler when persisting just the api reducer.
3838

3939
```ts title="redux-persist rehydration example"
40-
import type { Action, PayloadAction } from '@reduxjs/toolkit'
40+
import type { Action } from '@reduxjs/toolkit'
4141
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
4242
import { REHYDRATE } from 'redux-persist'
4343

4444
type RootState = any // normally inferred from state
4545

46-
function isHydrateAction(action: Action): action is PayloadAction<RootState> {
46+
function isHydrateAction(action: Action): action is Action<typeof REHYDRATE> & {
47+
key: string
48+
payload: RootState
49+
err: unknown
50+
} {
4751
return action.type === REHYDRATE
4852
}
4953

5054
export const api = createApi({
5155
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
5256
// highlight-start
53-
extractRehydrationInfo(action, { reducerPath }) {
57+
// to prevent circular type issues, the return type needs to be annotated as any
58+
extractRehydrationInfo(action, { reducerPath }): any {
5459
if (isHydrateAction(action)) {
55-
if ((action as any).key === 'key used with redux-persist') {
56-
// when persisting the api reducer
60+
// when persisting the api reducer
61+
if (action.key === 'key used with redux-persist') {
5762
return action.payload
5863
}
5964

6065
// When persisting the root reducer
61-
return action.payload[reducerPath]
66+
return action.payload[api.reducerPath]
6267
}
6368
},
6469
// highlight-end

packages/toolkit/src/query/createApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export interface CreateApiOptions<
182182
* export const api = createApi({
183183
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
184184
* // highlight-start
185-
* extractRehydrationInfo(action, { reducerPath }) {
185+
* extractRehydrationInfo(action, { reducerPath }): any {
186186
* if (isHydrateAction(action)) {
187187
* return action.payload[reducerPath]
188188
* }

0 commit comments

Comments
 (0)