Skip to content

Commit 434a86c

Browse files
committed
add docs for addAsyncThunk
1 parent 3c6de47 commit 434a86c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
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/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,

0 commit comments

Comments
 (0)