[英]redux-thunk or middleware?
I've multiple action
after which I have to call other 2 action
. 我有多个
action
之后我必须调用其他两个action
。
My first approach would be to write a middleware
that looks like this: 我的第一种方法是编写一个如下所示的
middleware
:
some-middleware.js 一些-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)
}
Another approach would be to put 另一种方法是
store.dispatch(resetStories())
store.dispatch(getStories())
inside two redux-thunk
like 里面两个
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())
}
This means repeated code per every time I need those two actions to be dispatched. 这意味着每当我需要分派这两个动作时,便会重复代码。
Am I missing something about all this? 我是否对所有这些都有所遗漏? Is middleware approach correct?
中间件方法正确吗?
You can setup a base dispatch function with redux-thunk. 您可以使用redux-thunk设置基本调度功能。 Still a little extra code, and if you have multiple action files you'll need to import the function into all of them, but I think the app will be more robust since you might encounter a situation when you don't want to dispatch those actions.
还有一些额外的代码,如果您有多个操作文件,则需要将功能导入到所有文件中,但是我认为该应用程序会更强大,因为您可能会遇到不想分派这些文件的情况动作。
export const baseDispatch = (action) => (dispatch) => {
dispatch(action)
dispatch(resetStories())
dispatch(getStories())
}
export const postCategories = (categories) => (dispatch) => {
dispatch(baseDispatch(fetchPostCategories(categories)));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.