React Router Redux不可变

时间:2016-04-29 02:31:10

标签: javascript reactjs redux immutable.js react-router-redux

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

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

  

未捕获的TypeError:无法读取属性' 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;
};

1 个答案:

答案 0 :(得分:1)

根据文档,建议使用routing作为reducer键。因此,您基本上尝试使用密钥routing而不是route

访问商店

我只是尝试了它而没有不可变,如下所示,

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

这会回来, enter image description here

希望有所帮助