2016-04-29 36 views
1

Seit einer Weile verwenden BoilerPlate und habe plain JS-Objekte als den Speicher/Zustand verwendet, bis ich einige ungerade Mutationen nach dem Zufallsprinzip begann, so entschied sich, zu Immutable.js zu wechseln. Zur Zeit versuche ich redux-immutable in meinen Code zu implementieren. Ich habe ein Problem mit react-router-redux und redux-immutable; Leider kenne ich diese Bibliotheken nicht sehr gut 'unter der Haube', also habe ich eine Menge Probleme beim Debuggen. Ich habe die Anweisungen in der redux-immutable README dazu befolgt.React Router Redux unveränderbar

Erhalte diesen Fehler in meiner index.js Datei.

Uncaught TypeError: Cannot read property 'toJS' of undefined

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 (Routing Minderer)

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; 
}; 
+0

können Sie versuchen, mit 'return state.getIn ([ 'routing', 'Standort' ]). ToJ S(); 'statt' return state.getIn ([ 'route', 'location' ]). ToJS(); ' – anoop

Antwort

0

Gemäß der Dokumentation ist es vorgeschlagen routing als Reduktions Schlüssel zu verwenden. So, hier sind Sie versuchen im Grunde das Geschäft mit der Schlüssel routing stattdessen den Zugriff von route

Ich habe gerade versucht es ohne unveränderlich, und ist als unten,

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

Dies wird zurückkehren, enter image description here

Hoffe, es hilft

Verwandte Themen