我如何在 Redux 中的另一个 thunk 中使用 thunk?

发布于 2025-01-19 13:02:45 字数 1740 浏览 1 评论 0 原文

我通过 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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2025-01-26 13:02:45

他的问题解决了,你需要抛出一个空对象,如果你甚至没有任何参数,那么他被调用

his problem is solved, you need to throw an empty object, if you don't even have any arguments, then he is called

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文