Skip to content

Commit 756f99c

Browse files
committed
Update createAsyncThunk.mdx
Update docs to include a typing on the user argument.
1 parent cbdd25e commit 756f99c

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

docs/api/createAsyncThunk.mdx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,33 @@ Redux Toolkit's [**RTK Query data fetching API**](../rtk-query/overview.md) is a
2525

2626
Sample usage:
2727

28-
```js {5-11,22-25,30}
28+
```ts {5-11,22-25,30}
2929
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
3030
import { userAPI } from './userAPI'
3131

3232
// First, create the thunk
3333
const fetchUserById = createAsyncThunk(
3434
'users/fetchByIdStatus',
35-
async (userId, thunkAPI) => {
35+
async (userId: number, thunkAPI) => {
3636
const response = await userAPI.fetchById(userId)
3737
return response.data
3838
}
3939
)
4040

41+
interface UsersState {
42+
entities: []
43+
loading: 'idle' | 'pending' | 'succeeded' | 'failed'
44+
}
45+
46+
const initialState = {
47+
entities: [],
48+
loading: 'idle',
49+
} as UsersState
50+
4151
// Then, handle actions in your reducers:
4252
const usersSlice = createSlice({
4353
name: 'users',
44-
initialState: { entities: [], loading: 'idle' },
54+
initialState,
4555
reducers: {
4656
// standard reducer logic, with auto-generated action types per reducer
4757
},

0 commit comments

Comments
 (0)