【发布时间】:2019-01-09 17:38:41
【问题描述】:
我有多个action,之后我必须致电其他 2 个action。
我的第一种方法是编写一个如下所示的middleware:
some-middleware.js
const actions = {
[POST_CATEGORIES_SUCCESS]: 1,
[SET_READING_TIME_SUCCESS]: 1
}
export default store => next => action => {
// console.log("calling auth middleware", action)
// Reset any session data before SIGNUP or LOGIN request
if (actions[action.type] === 1) {
store.dispatch(resetStories())
store.dispatch(getStories())
}
return next(action)
}
另一种方法是放
store.dispatch(resetStories())
store.dispatch(getStories())
里面两个redux-thunk点赞
export const postCategories = (categories) => (dispatch) => {
dispatch(fetchPostCategories(categories))
store.dispatch(resetStories())
store.dispatch(getStories())
}
export const setReadingTime = (readingTime) => (dispatch) => {
dispatch(fetchReadingTime(readingTime))
store.dispatch(resetStories())
store.dispatch(getStories())
}
这意味着每次我需要调度这两个操作时都会重复代码。
我错过了这一切吗?中间件方法是否正确?
【问题讨论】:
标签: reactjs redux redux-thunk