redux-thunk 和 redux-promise 有什么区别?
据我所知,如果我错了,请纠正我,redux-thunk 是一个帮助我们调度的中间件当我使用 redux-promise 时,操作本身中的异步函数和调试值我无法在不实现我的情况下创建异步函数自己的机制,因为 Action 会抛出仅调度普通对象的异常.
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 redux-promise I couldn't create async functions without implementing my own mechanism as Action throws an exception of dispatching only plain objects.
这两个包的主要区别是什么?在单页反应应用程序中使用这两个包或坚持使用 redux-thunk 是否有任何好处?
What is the major differences between these two packages? Are there any benefits of using both the packages in a single page react app or sticking to redux-thunk would be enough?
redux-thunk
允许你的动作创建者返回一个函数:
redux-thunk
allows your action creators to return a function :
function myAction(payload){
return function(dispatch){
// use dispatch as you please
}
}
redux-promise
允许他们返回一个承诺:
redux-promise
allows them to return a promise :
function myAction(payload){
return new Promise(function(resolve, reject){
resolve(someData); // redux-promise will dispatch someData
});
}
如果您需要异步或有条件地分派操作,这两个库都很有用.redux-thunk
还允许您在一个动作创建者中多次分派.您是选择一个、另一个还是两者都完全取决于您的需求/风格.
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.