Custom Redux Middleware - Dispatch to beginning of middleware chain?

后端 未结 2 814
醉话见心
醉话见心 2021-01-20 08:01

I am writing a custom middleware that needs to dispatch thunk actions. The problem is that the middleware is called after redux-thunk in the middleware chain,

2条回答
  •  执笔经年
    2021-01-20 08:28

    It turns out the issue was in my store configuration. Using redux's compose caused the issue.

    before:

    import {createStore, applyMiddleware, compose} from 'redux';
    import thunk from 'redux-thunk';
    import rootReducer from '../redux/reducers';
    import webrtcVideoMiddleware from '../redux/middleware/webrtcVideo';
    import bugsnagErrorCatcherMiddleware from '../redux/middleware/bugsnag/errorCatcher';
    import bugsnagbreadcrumbLoggerMiddleware from '../redux/middleware/bugsnag/breadcrumbLogger';
    import * as APIFactory from '../services/APIFactory';
    import Pusher from '../services/PusherManager';
    
    const PusherManager = new Pusher(false);
    
    export default function configureStore(initialState) {
        return createStore(rootReducer, initialState, compose(
            applyMiddleware(bugsnagErrorCatcherMiddleware()),
            applyMiddleware(thunk.withExtraArgument({APIFactory, PusherManager})),
            applyMiddleware(webrtcVideoMiddleware(PusherManager)),
            applyMiddleware(bugsnagbreadcrumbLoggerMiddleware())
        ));
    }
    

    after:

    import {createStore, applyMiddleware} from 'redux';
    import thunk from 'redux-thunk';
    import rootReducer from '../redux/reducers';
    import webRTCVideoMiddleware from '../redux/middleware/webrtcVideo';
    import bugsnagErrorCatcherMiddleware from '../redux/middleware/bugsnag/errorCatcher';
    import bugsnagBreadcrumbLoggerMiddleware from '../redux/middleware/bugsnag/breadcrumbLogger';
    import * as APIFactory from '../services/APIFactory';
    import Pusher from '../services/PusherManager';
    
    const PusherManager = new Pusher(false);
    
    export default function configureStore(initialState) {
        const middleware = [
            bugsnagErrorCatcherMiddleware(),
            thunk.withExtraArgument({APIFactory, PusherManager}),
            webRTCVideoMiddleware.withExtraArgument(PusherManager),
            bugsnagBreadcrumbLoggerMiddleware(),
        ];
    
        return createStore(rootReducer, initialState, applyMiddleware(...middleware));
    }
    

提交回复
热议问题