【发布时间】:2018-06-12 07:23:01
【问题描述】:
我正在创建一个监听特定操作的 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))
);
我遇到的问题是这些操作没有调度。当我 console.log 所有动作时,它们会继续正常运行,而不是调度新动作。就好像调度这些动作的调用被忽略了。我在这里做错了什么?谢谢!
【问题讨论】:
-
你有没有看到顶部的
console.log? -
@Krasimir 是的。它记录了每一个动作,所以我看到它忽略了我想要调度的动作。
-
我认为您不会看到
NEW_ACTION注销,因为这不是通过相同的功能。它被发送到减速器。您可以尝试将 console.log 放在您的减速器中,看看您是否得到NEW_ACTION而不是UNIQUE_ACTION_A例如。
标签: redux