2

我创建了一个这样的中间件:

export default store => next => action => {
    const res = next(action)

    console.log("Middleware", action.type)

    return res
} 

我的商店配置是:

import { createBrowserHistory } from 'history';
import { applyMiddleware, compose, createStore } from 'redux';
import { routerMiddleware } from 'connected-react-router';
import middleware from './middleware'

import rootReducer from './reducers';

export const history = createBrowserHistory();

const defaultState = {};

const composeEnhancers =
  typeof window === 'object' &&
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?   
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
    }) : compose;

const enhancer = composeEnhancers(
  applyMiddleware(middleware, routerMiddleware(history)),
);

export const store = createStore(
  rootReducer(history),
  defaultState,
  enhancer
);

我还有一个减速器,我在日志和调试器中看到减速器首先被调用,然后是中间件。关于我配置错误的任何建议?

4

1 回答 1

4

如果我没记错的话,当你调用 reducer 时会调用它next(action)。将你移到console.log上面,那应该会给你你想要的日志顺序。

于 2019-11-16T15:12:52.393 回答