2

使用这个样板已经有一段时间了,并且一直使用普通的 JS 对象作为存储/状态,直到我开始随机获得一些奇怪的突变,所以决定切换到 Immutable.js。目前我正在尝试在我的代码中实现redux-immutable 。我对 react-router-redux 和 redux-immutable 有一些问题;不幸的是,我不太了解这些库的“幕后”,因此在调试时遇到了很多麻烦。我已按照 redux-immutable README 中的说明进行操作。

在我的 index.js 文件中出现此错误。

未捕获的类型错误:无法读取未定义的属性“toJS”

index.js

const initialState = Immutable.Map({});

const store = configureStore(initialState);
const history = syncHistoryWithStore(hashHistory, store, {
  selectLocationState (state) {
    return state.getIn([
      'route',
      'location'
    ]).toJS();
  }
});

render(
  <Provider store={store}>
    <Router history={history} routes={routes} />
  </Provider>,
  document.getElementById('root')
);

configureStore.js

const router = routerMiddleware(hashHistory);

const enhancer = compose(
  applyMiddleware(thunk, router, logger),
  DevTools.instrument(),
  persistState(
    window.location.href.match(
      /[?&]debug_session=([^&]+)\b/
    )
  )
);

export default function configureStore(initialState) {
  const store = createStore(rootReducer, initialState, enhancer);

  if (module.hot) {
    module.hot.accept('../reducers', () =>
      store.replaceReducer(require('../reducers'))
    );
  }

  return store;
}

rootReducer.js

import {combineReducers} from 'redux-immutable';

import routing from './Routing';
import Graphing from './Graphing';
import Syncing from './Syncing';
import Navigating from './Navigating';
import Notifying from './Notifying';

const rootReducer = combineReducers({
  routing,
  Graphing,
  Navigating,
  Syncing,
  Notifying
});

export default rootReducer;

routing.js(路由减速器)

import {LOCATION_CHANGE} from 'react-router-redux';

const initialState = Immutable.fromJS({
  location: {}
});

export default (state = initialState, action) => {
  if (action.type === LOCATION_CHANGE) {
    return state.merge({
      location: action.payload
    });
  }

  return state;
};
4

1 回答 1

1

根据文档,建议将其routing用作减速键。因此,在这里您基本上是在尝试使用密钥routing而不是route

我只是在没有不可变的情况下尝试了它,如下所示,

const history = syncHistoryWithStore(browserHistory, store, {
  selectLocationState(state) {
    console.log(state.routing.locationBeforeTransitions);
    return state.routing;
  }
});

这将返回, 在此处输入图像描述

希望能帮助到你

于 2016-04-29T03:15:28.813 回答