'Is it good to set a condition based on action type inside a custom redux middleware?
I'm going to create a custom redux. And based on the different scenarios, I'll do some extra inside the middleware.
I have two options to detect check the condition. Option 1 is checking the action type and based on the action type, to do the extra action.
const customMiddleware= store => next => action => {
const { type } = action
const extraActionList = [ "TYPE1", "TYPE2", "TYPE3", "TYPE4", ..., ] // adding all extra action here
if (extraActionList.includes(type)) {
.... something extra happen here
}
return next(action)
}
Another option is instead of set condition inside middleware, I will do something on the action, I add some extra property to the original action. So the custom middleware will be:
const customMiddleware= store => next => action => {
const { extraAction } = action
if (extraAction) {
.... something extra happen here
}
return next(action)
}
for actions, if the original action object is { type: 'TYPE1', payload: 'abc' }, with those I want to do something extra, I will modify it to { type: 'TYPE1', payload: 'abc', extraAction: true }. But for those not with extra, I will leave them as the old, like { type: 'TYPE2', payload: 'abcd' } without the extra property.
I want to know which way is better.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
