我如何在 Redux 中的另一个 thunk 中使用 thunk?
我通过 redux 工具包“createAsyncThunk”创建了 thunk,并且在这里做了一些异步操作
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)
}
})
,但有一刻我需要使用另一个像“HeaderLogin”这样的 thunk:
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
}
})
}
})
但 thunk 没有被调用。 也许有什么方法可以调用 thunk 吗?
如何在 Redux 中的另一个 thunk 中使用 thunk?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他的问题解决了,你需要抛出一个空对象,如果你甚至没有任何参数,那么他被调用
his problem is solved, you need to throw an empty object, if you don't even have any arguments, then he is called