Skip to content

Commit d02c27a

Browse files
add docs for addAsyncThunk (#5066)
* add docs for addAsyncThunk * export asyncthunkreducers type * Tweak test timing to fix flakes --------- Co-authored-by: Mark Erikson <mark@isquaredsoftware.com>
1 parent ab346b9 commit d02c27a

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

docs/api/createReducer.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ const counterReducer = createReducer(initialState, (builder) => {
9090

9191
[params,examples](docblock://mapBuilders.ts?token=ActionReducerMapBuilder.addCase)
9292

93+
### `builder.addAsyncThunk`
94+
95+
[summary,remarks](docblock://mapBuilders.ts?token=ActionReducerMapBuilder.addAsyncThunk)
96+
97+
#### Parameters
98+
99+
[params,examples](docblock://mapBuilders.ts?token=ActionReducerMapBuilder.addAsyncThunk)
100+
93101
### `builder.addMatcher`
94102

95103
[summary,remarks](docblock://mapBuilders.ts?token=ActionReducerMapBuilder.addMatcher)

packages/toolkit/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export type {
104104
export type {
105105
// types
106106
ActionReducerMapBuilder,
107+
AsyncThunkReducers,
107108
} from './mapBuilders'
108109
export { Tuple } from './utils'
109110

packages/toolkit/src/mapBuilders.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,31 @@ export interface ActionReducerMapBuilder<State> {
7373
* All calls to `builder.addAsyncThunk` must come before after any calls to `builder.addCase` and before any calls to `builder.addMatcher` or `builder.addDefaultCase`.
7474
* @param asyncThunk - The async thunk action creator itself.
7575
* @param reducers - A mapping from each of the `AsyncThunk` action types to the case reducer that should handle those actions.
76+
* @example
77+
```ts no-transpile
78+
import { createAsyncThunk, createReducer } from '@reduxjs/toolkit'
79+
80+
const fetchUserById = createAsyncThunk('users/fetchUser', async (id) => {
81+
const response = await fetch(`https://reqres.in/api/users/${id}`)
82+
return (await response.json()).data
83+
})
84+
85+
const reducer = createReducer(initialState, (builder) => {
86+
builder.addAsyncThunk(fetchUserById, {
87+
pending: (state, action) => {
88+
state.fetchUserById.loading = 'pending'
89+
},
90+
fulfilled: (state, action) => {
91+
state.fetchUserById.data = action.payload
92+
},
93+
rejected: (state, action) => {
94+
state.fetchUserById.error = action.error
95+
},
96+
settled: (state, action) => {
97+
state.fetchUserById.loading = action.meta.requestStatus
98+
},
99+
})
100+
})
76101
*/
77102
addAsyncThunk<
78103
Returned,

packages/toolkit/src/query/tests/optimisticUpserts.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const api = createApi({
8686
// and leave a side effect we can check in the test
8787
api.dispatch(postAddedAction(res.data.id))
8888
},
89-
keepUnusedDataFor: 0.01,
89+
keepUnusedDataFor: 0.1,
9090
}),
9191
getFolder: build.query<FolderT, number>({
9292
queryFn: async (args) => {
@@ -430,12 +430,12 @@ describe('upsertQueryEntries', () => {
430430

431431
expect(selectedData).toBe(posts[0])
432432
},
433-
{ timeout: 50, interval: 5 },
433+
{ timeout: 150, interval: 5 },
434434
)
435435

436436
// The cache data should be removed after the keepUnusedDataFor time,
437437
// so wait longer than that
438-
await delay(100)
438+
await delay(300)
439439

440440
const stateAfter = storeRef.store.getState()
441441

0 commit comments

Comments
 (0)