dpw*_*ell 5 javascript middleware reactjs redux
我有一个问题,其中增强器(在这种情况下为redux-little-router)吞并了一个动作并调度了一个新动作。我也有中间件,该中间件需要侦听此新操作(以及其有效负载)并作为响应调度异步操作。
这是不可能的,因为增强器位于链中的applyMiddleware之后(应该如此),因此分派的新操作仅通过后续增强器的分派功能进行。
这是一个人为的示例:
export const testMiddleware = store => next => action => {
console.log('Middleware1', action.type);
if (action.type === 'SOME_OTHER_ACTION_TYPE') {
console.log('Hurray, responded to some other action here!');
}
return next(action);
};
function testEnhancer() {
return(createStore) => (reducer, preloadedState, enhancer) => {
const store = createStore(reducer, preloadedState, enhancer);
const dispatch = (action) => {
// Swallow this action, and dispatch the replacement
if (action.type === 'SOME_ACTION_TYPE') {
console.log('testEnhancer', action.type, 'swallowing');
store.dispatch({
type: 'SOME_OTHER_ACTION_TYPE',
payload: 'some other payload'
});
return null;
}
console.log('testEnhancer', action.type, 'passing on');
return store.dispatch(action);
}
return Object.assign({}, store, {dispatch});
};
}
export default function configureStore(initialState = undefined) {
const enhancer = compose(
applyMiddleware(
thunkMiddleware,
testMiddleware
),
testEnhancer(),
DevTools.instrument()
);
Run Code Online (Sandbox Code Playgroud)
看来这是不可能的情况,但是我找不到任何文档说明增强程序不应调度动作。也许他们不应该?还是有解决方法?
| 归档时间: |
|
| 查看次数: |
842 次 |
| 最近记录: |