What is the difference between redux-thunk and redux-promise?

前端 未结 3 1774
执笔经年
执笔经年 2020-12-04 06:18

As far as I know and correct me if I am wrong, redux-thunk is a middleware which helps us dispatch async function and debug values in the action itself while when I used red

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 06:45

    redux-thunk allows your action creators to return a function :

    function myAction(payload){
        return function(dispatch){
            // use dispatch as you please
        }
    }
    

    redux-promise allows them to return a promise :

    function myAction(payload){
        return new Promise(function(resolve, reject){
            resolve(someData); // redux-promise will dispatch someData
        });
    }
    

    Both libraries are useful if you need to dispatch action async or conditionally. redux-thunk also allows you to dispatch several times within one action creator. Whether you choose one, the other or both entirely depends on your needs/style.

提交回复
热议问题