我正在创建一个侦听特定操作的Redux中间件。如果该动作类型与我正在寻找的匹配,我想发送另一个动作。这是因为我有不同的组件和一些共享功能,所以我想更新动作以具有相似类型,但不同的有效负载(术语),如下所示:
const updateActions = store => next => action => {
console.log(action);
if (action.type === 'UNIQUE_ACTION_A') {
return next({ type: 'NEW_ACTION', term: 'new_action_a_test' });
} else if (action.type === 'UNIQUE_ACTION_B') {
return next({
type: 'NEW_ACTION',
term: 'new_action_b_test'
});
}
return next(action);
};
const store = createStore(
rootReducer,
composeWithDevTools(applyMiddleware(thunk, updateActions))
);
我遇到的问题是这些操作不是调度。当我调度所有操作时,它们会继续正常运行,而不是调度新操作。就像调用这些动作的调用被忽略一样。我在这里做错了什么?谢谢!
中间件中的next
和dispatch
有区别。 dispatch
在调度链的最开始发送一个动作,这将导致它运行管道中的所有中间件。 next
在此之后将其发送到下一个中间件,并最终发送给Reducer。通过调用next({type : "NEW_ACTION"})
,你将它发送到下一个中间件,这个中间件永远不会看到"NEW_ACTION"
。