This repository was archived by the owner on Oct 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 637
This repository was archived by the owner on Oct 26, 2018. It is now read-only.
immutablejs instructions #301
Copy link
Copy link
Closed
Description
I'm working from https://github.com/davezuko/react-redux-starter-kit if that helps explain my context.
This is the custom reducer I needed:
import Immutable from 'immutable';
import {
LOCATION_CHANGE
} from 'react-router-redux';
let initialState;
initialState = Immutable.fromJS({
locationBeforeTransitions: undefined
});
export default (state = initialState, action) => {
if (action.type === LOCATION_CHANGE) {
return state.merge({
locationBeforeTransitions: action.payload
});
}
return state;
};
I had to read the following to figure this out:
- Officially document @@router/LOCATION_CHANGE #298
- https://github.com/reactjs/react-router-redux/blob/v4.0.0/src/reducer.js
Also, I had to set up a custom "selectLocationState" selector to get immutable store values to be fed to Router correctly:
import createHistory from 'history/lib/createHashHistory';
// or: import createHistory from 'history/lib/createBrowserHistory';
import { useRouterHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
const createSelectLocationState = () => {
let prevRoutingState, prevRoutingStateJS;
return (state) => {
const routingState = state.get('routing'); // or state.routing
if (typeof prevRoutingState === 'undefined' || prevRoutingState !== routingState) {
prevRoutingState = routingState;
prevRoutingStateJS = routingState.toJS();
}
return prevRoutingStateJS;
};
};
const browserHistory = useRouterHistory(createHistory)({
basename: __BASENAME__
});
const history = syncHistoryWithStore(browserHistory, store, {
selectLocationState: createSelectLocationState()
});
// pass above "history" to Router component as a prop
That's mentioned here:
Update: added createSelectLocationState
fix
Metadata
Metadata
Assignees
Labels
No labels