【发布时间】:2016-05-28 04:53:10
【问题描述】:
我在 reducer 中调用了一个 promise 函数,所以我这样称呼它:
cart(state, action) {
switch(action.type) {
//...
case LOG_IN:
return getCartProducts(true)
}
}
getCartProducts(isLogin) {
// ....
return cartApi.list({
customerId: "",
items: items
}).then(data => {
return cartReduce(undefined, receiveCartProducts(data.items, false))
})
}
所以这只是返回一个承诺,而不是我想要的对象
【问题讨论】:
-
reducer 应该是纯的,你不应该在那里请求东西。
-
@QoP 所以如果我想听
LOG_IN的动作,我该怎么办? -
看看 redux-thunk 作为管理异步请求的一种可能机制。您基本上会触发多个操作来表示异步请求状态。也强烈推荐你阅读官方的 redux 文档。
标签: reactjs promise redux reducers