reactjs/react-router-redux

How does `selectLocationState` work? Give examples

wzup opened this issue · 3 comments

wzup commented

Can you elaborate on that? It is not clear

selectLocationState - (default state => state.routing) A selector function to obtain the history state from your store. Useful when not using the provided routerReducer to store history state. Allows you to use wrappers, such as Immutable.js.

You say A selector function to obtain the history state. Ok, here's your function. But what to do with it? It throws. Give some examples.

const history = syncHistoryWithStore(browserHistory, store, {selectLocationState: (state) => {
    // I have state here. What's next?
}});

It's an opportunity to select the location from state if you don't use the provided reducer, or it stores in state in a way the default selector won't work.

wzup commented

@timdorr
Can you provide example?
Your docs are obscure and examples provided in docs throw

const history = syncHistoryWithStore(browserHistory, store, {selectLocationState: (state) => {
    return state.routing;
}});

// These both options throw
let rootReducer = reduceReducers(
    // This throws
    combineReducers({
        routing: routerReducer,
    }),
    reducerA,
    reducerB,
    // This also throws
    (state = {'locationBeforeTransitions': null}, {type, payload}) => {
        if (type === 'LOCATION_CHANGE') {
            return state.locationBeforeTransitions = payload;
        }
        return state
    }
)

Error

Uncaught Error: Expected the routing state to be available either as state.routing or as the custom expression you can specify as selectLocationState in the syncHistoryWithStore() options. Ensure you have added the routerReducer to your store's reducers via combineReducers or whatever method you use to isolate your reducers.