File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -25,23 +25,33 @@ Redux Toolkit's [**RTK Query data fetching API**](../rtk-query/overview.md) is a
25
25
26
26
Sample usage:
27
27
28
- ``` js {5-11,22-25,30}
28
+ ``` ts {5-11,22-25,30}
29
29
import { createAsyncThunk , createSlice } from ' @reduxjs/toolkit'
30
30
import { userAPI } from ' ./userAPI'
31
31
32
32
// First, create the thunk
33
33
const fetchUserById = createAsyncThunk (
34
34
' users/fetchByIdStatus' ,
35
- async (userId , thunkAPI ) => {
35
+ async (userId : number , thunkAPI ) => {
36
36
const response = await userAPI .fetchById (userId )
37
37
return response .data
38
38
}
39
39
)
40
40
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
+
41
51
// Then, handle actions in your reducers:
42
52
const usersSlice = createSlice ({
43
53
name: ' users' ,
44
- initialState: { entities : [], loading : ' idle ' } ,
54
+ initialState ,
45
55
reducers: {
46
56
// standard reducer logic, with auto-generated action types per reducer
47
57
},
You can’t perform that action at this time.
0 commit comments