'How i can use thunk inside another thunk in Redux?
I created thunk by redux toolkit "createAsyncThunk" and i do some async operations here
export let Login: any = createAsyncThunk("authPage/Login",
async ({values, action}: any, {dispatch, rejectWithValue}) => {
try {
let response = await AuthAPI.Login(values)
if (response.data.resultCode === 0) {
let responseFromAuthMe = await HeaderAPI.AuthMe()
if (responseFromAuthMe.data.resultCode === 0) {
- - - - - - dispatch(HeaderLogin()) - - - - - - - - -
}
} else {
if (response.data.resultCode === 10) {
let response = AuthAPI.GetCaptcha()
dispatch(getCaptcha(response))
}
action.setStatus({error: response.data.messages})
}
} catch (err: any) {
return rejectWithValue(err.response.data)
}
})
but one moment i need use another thunk like "HeaderLogin" :
export let HeaderLogin: any = createAsyncThunk("authPage/HeaderLogin",
async ({}, {dispatch}) => {
let responseFromAuthMe = await HeaderAPI.AuthMe()
if (responseFromAuthMe.data.resultCode === 0) {
let {id, email, login} = responseFromAuthMe.data.data;
//TODO check 4 arg - isLogin
dispatch(setAuthUserData({id, email, login}))
let responseLogin = await HeaderAPI.Login(login)
return responseLogin.data.items.filter((u: any) => {
if (id === u.id) {
return u
}
})
}
})
but thunk is not called. maybe have any way that calls thunk?
How i can use thunk inside another thunk in Redux?
Solution 1:[1]
his problem is solved, you need to throw an empty object, if you don't even have any arguments, then he is called
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yefimchuk |
