简体   繁体   English

如何使用thunk和useDispatch(react-redux hooks)从动作返回一个promise?

[英]How to return a promise from an action using thunk and useDispatch (react-redux hooks)?

I just started exploring react-redux hooks and I was curious how to return a promise if I am using thunk and useDispatch() . 我刚刚开始探索react-redux钩子,如果我使用thunk和useDispatch() ,我很好奇如何返回一个promise。 Essentially I want to achieve the following: 基本上我想实现以下目标:

const dispatch = useDispatch();

dispatch(myAction(...args)).then((result) => {
    ...do something with result
});

When my action looks like this: 当我的动作看起来像这样:

const myAction = (arg1, arg2) => {
    return (dispatch, getState) => {
        Promise.resolve(arg1 + arg2);
    }
}

I've simplified my problem a lot, but that is essentially what I'm dealing with. 我已经简化了我的问题,但这基本上就是我正在处理的问题。 When I try to dispatch the above action, I get the error dispatch(...).then is not a function. 当我尝试调度上述操作时,我得到错误dispatch(...).then不是函数。

I know redux hooks are pretty new, but I was curious if anybody had gotten this to work or would know a solution. 我知道redux挂钩是相当新的,但我很好奇,如果有人让这个工作或知道解决方案。 I feel like it should be relatively easy to make this work, but I am at a loss. 我觉得做这项工作应该相对容易,但我很茫然。 If you need any more information, let me know. 如果您需要更多信息,请与我们联系。 Thanks in advance for any help! 在此先感谢您的帮助!

As dispatch returns either of two: 由于dispatch返回两个中的任何一个:

  1. For sync action (like dispatch ({type: 'ACTION'}) it will return action object ( {type: 'ACTION'} in my example) 对于同步操作(如dispatch ({type: 'ACTION'}) ,它将返回操作对象(在我的示例中为{type: 'ACTION'}

  2. For thunk actions (action creators which return functions) it returns the same result returned from action creator. 对于thunk动作(返回函数的动作创建者),它返回从动作创建者返回的相同结果。

So for your case just add return statement for your action creator 因此,对于您的情况,只需为您的操作创建者添加return语句

const myAction = (arg1, arg2) => {
    return (dispatch, getState) => {
        return Promise.resolve(arg1 + arg2);
    }
}

You can make myAction more realistic like so 你可以让myAction更加逼真

const myAction = (arg1, arg2) => {
    return (dispatch, getState) => {
        return fetch(/* some request */).then(response => dispatch ({type: 'RESPONSE_RECEIVED', payload: response}));
    }
}

In this case also resolved promise will be returned. 在这种情况下,还将返回已解决的承诺。 Contnents of promise will be object {type: 'RESPONSE_RECEIVED', payload: response} . promise的成员将是对象{type: 'RESPONSE_RECEIVED', payload: response}

Or you can set arbitrary contents for returned promise like so 或者您可以为返回的promise设置任意内容

const myAction = (arg1, arg2) => {
    return (dispatch, getState) => {
        return fetch(/* some request */).then(response => { 
            dispatch ({type: 'RESPONSE_RECEIVED', payload: response})
            return response;
        }
    }
}

In this example resolved promise will be returned but which contains response inside. 在此示例中,将返回已解析的promise,但其中包含response

In all cases you can chain like you want 在所有情况下,您都可以按照自己的意愿进

dispatch(myAction(...args)).then((result) => {
    ...do something with result
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用react-redux返回一个简单的动作创建者的承诺 - Return a promise for a simple action creator in react using react-redux Redux thunk:从派遣行动中返回承诺 - Redux thunk: return promise from dispatched action 如何从 redux thunk 操作返回一个承诺并在组件中使用它 - How to return a promise from redux thunk action and consume it in component 从React Redux Thunk返回承诺 - Return promise from React Redux Thunk 如何输入Redux重击动作创建者以返回承诺 - How can I type a Redux thunk action creator to return a promise cb && 'function' === typeof cb && cb(data) 如何在带有 redux-thunk 的 Action(React-redux) 中工作? - how does cb && 'function' === typeof cb && cb(data) work in the Action(React-redux) with redux-thunk? 尝试导入错误:“useDispatch”未从“react-redux”导出 - Attempted import error: 'useDispatch' is not exported from 'react-redux' 努力使用 react-redux 钩子和 state - Struggling using react-redux hooks with state 如何使用 react-redux connect() 从类组件访问 redux-toolkit reducer/action? - How to access redux-toolkit reducer/action from a class component using react-redux connect()? Redux + 钩子 useDispatch() 在 useEffect 调用动作两次 - Redux + Hooks useDispatch() in useEffect calling action twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM