簡體   English   中英

React Router Redux 不可變

[英]React Router Redux Immutable

使用這個樣板已經有一段時間了,並且一直使用普通的 JS 對象作為存儲/狀態,直到我開始隨機獲得一些奇怪的突變,所以決定切換到 Immutable.js。 目前我正在嘗試在我的代碼中實現redux-immutable 我對 react-router-redux 和 redux-immutable 有一些問題; 不幸的是,我不太了解這些庫的“幕后”,因此在調試時遇到很多麻煩。 我已按照 redux-immutable README 中的說明進行操作。

在我的 index.js 文件中出現此錯誤。

未捕獲的類型錯誤:無法讀取未定義的屬性“toJS”

索引.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')
);

配置Store.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;
};

根據文檔,建議使用routing作為 reducer 鍵。 所以在這里你基本上是試圖與鍵進入商店routing ,而不是route

我只是在沒有不可變的情況下嘗試過,如下所示,

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

這將返回, 在此處輸入圖片說明

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM