0

我将创建一个自定义的 redux。并且根据不同的场景,我会在中间件内部做一些额外的事情。

我有两个选项来检测检查条件。选项 1 是检查动作类型并根据动作类型执行额外动作。

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)
}

另一种选择是代替在中间件中设置条件,我将对动作做一些事情,我在原始动作中添加一些额外的属性。所以自定义中间件将是:

const customMiddleware= store => next => action => {
    const { extraAction } = action
    if (extraAction) {
    .... something extra happen here
    }
    return next(action)
}

对于动作,如果原始动作对象是{ type: 'TYPE1', payload: 'abc' },对于那些我想做一些额外的事情,我会将其修改为{ type: 'TYPE1', payload: 'abc', extraAction: true }. 但是对于那些没有额外财产的人,我会让他们像{ type: 'TYPE2', payload: 'abcd' }没有额外财产一样旧。

我想知道哪种方式更好。

4

0 回答 0