Skip to content

Instantly share code, notes, and snippets.

@pvpshoot
Last active June 1, 2018 13:32
Show Gist options
  • Save pvpshoot/a24314c85b30c1cea3b5277f13af9594 to your computer and use it in GitHub Desktop.
Save pvpshoot/a24314c85b30c1cea3b5277f13af9594 to your computer and use it in GitHub Desktop.
RFR help
import {
applyMiddleware,
combineReducers,
compose,
createStore as createReduxStore,
} from 'redux';
import { connectRoutes } from 'redux-first-router';
import thunk from 'redux-thunk';
import createHistory from 'history/createBrowserHistory';
import { routesMap } from 'router';
import reducers from 'store/reducers';
const history = createHistory();
const routerOptions = {
initialDispatch: true,
onBeforeChange(dispatch, getState, action) {
// SOME ACTIONS HERE
console.log('===========================');
console.log('dispatch, getState, action', dispatch, getState, action);
console.log('===========================');
},
};
const {
reducer: locationReducer,
middleware: routerMiddleware,
enhancer: routerEnhancer,
} = connectRoutes(history, routesMap, routerOptions);
const createStore = (initialState = {}) => {
// ======================================================
// Middleware Configuration
// ======================================================
const middleware = [routerMiddleware, thunk];
// ======================================================
// Store Enhancers
// ======================================================
const enhancers = [routerEnhancer];
let composeEnhancers = compose;
/* eslint-disable no-underscore-dangle */
if (process.env.NODE_ENV === 'development') {
if (typeof window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ === 'function') {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
}
}
/* eslint-enable no-underscore-dangle */
// ======================================================
// Store Instantiation
// ======================================================
const allReducers = {
location: locationReducer,
...reducers,
};
const store = createReduxStore(
combineReducers(allReducers),
initialState,
composeEnhancers(applyMiddleware(...middleware), ...enhancers)
);
return store;
};
export default createStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment