2

我一直在写一些自定义中间件。我只想看到我的商店(作为不同状态的连锁店)。每个减速器都有自己的状态,结合起来应该给我我的商店。

import {createStore, applyMiddleware, compose} from 'redux';
import rootReducer from '../reducers';
import thunk from 'redux-thunk';
import {createLogger} from 'redux-logger';
import PouchDB from 'pouchdb';

const logger = createLogger({collapsed: true});

const customMiddleware = store => next => action => {
  console.log(store.getState());
  next(action);
}

export default function configureStore(initialState) {
    return createStore(rootReducer, applyMiddleware(customMiddleware));
}

这就是我现在得到的:

这是我期望得到的:

添加 isImmutable 后,我尝试了这个:

const customMiddleware = store => next => action => {
  const state = store.getState();
  const storeTest = store.getState().toJS();
  console.log(storeTest);
  console.log(isImmutable(state) ? state.toJS() : state);
  next(action);
}

storeTest 给出了想要的结果,但其他日志没有。 知道如何解决这个问题吗?

4

1 回答 1

0

看起来您的 redux 状态是不可变 Map的而不是普通对象,因此您需要在记录之前对其进行转换:

import { isImmutable } from 'immutable';

..

const customMiddleware = store => next => action => {
  const state = store.getState();
  console.log(isImmutable(state) ? state.toJS() : state);
  next(action);
};
于 2018-04-23T13:27:35.233 回答