关于异步action的代码
来源:7-7 前端架构之抽象2:网络请求层封装( redux-thunk)(2)

慕虎9189897
2019-07-26
请问这段代码中,为什么需要第二个return,返回的dispatch(fetchLikes)交给谁使用了
export const actions = {
loadLikes: () => {
return (dispatch, getState) => {
const endpoint = url.getProductList(0, 10)
return dispatch(fetchLikes(endpoint))
}
},
}
写回答
1回答
-
你好,第二个return会返回一个Promise对象,因为会被api.js这个中间件处理,Promise包含了请求数据的结果,container层调用这个action时,可以这样写:
this.props.homeActions.loadLikes().then((res) => {
//do something
})
我们的项目中并没有这种调用场景,这样写更多的是一种redux最佳实践写法。
012019-07-29
相似问题