Skip to content

Commit

Permalink
Redux Store Refactor (#886)
Browse files Browse the repository at this point in the history
* Change the store to make adding middleware and enhancers more accommodating  (#602)

* wrapped the store creation all inside the default function

* set-up middleware and enhancers array to allow building what is included. For example the case of having a store specific to the main process vs the renderer process or even could be used to combine the development and production stores and add what is needed based on the ENV.

* Code style changes

* Fixed flow annotation
  • Loading branch information
amilajack authored Apr 1, 2017
1 parent 29528b9 commit 9f281c6
Showing 1 changed file with 40 additions and 28 deletions.
68 changes: 40 additions & 28 deletions app/store/configureStore.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,48 @@ import { hashHistory } from 'react-router';
import { routerMiddleware, push } from 'react-router-redux';
import createLogger from 'redux-logger';
import rootReducer from '../reducers';

import * as counterActions from '../actions/counter';
import type { counterStateType } from '../reducers/counter';

const actionCreators = {
...counterActions,
push,
};

const logger = createLogger({
level: 'info',
collapsed: true
});

const router = routerMiddleware(hashHistory);

// If Redux DevTools Extension is installed use it, otherwise use Redux compose
/* eslint-disable no-underscore-dangle */
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// Options: http://extension.remotedev.io/docs/API/Arguments.html
actionCreators,
}) :
compose;
/* eslint-enable no-underscore-dangle */
const enhancer = composeEnhancers(
applyMiddleware(thunk, router, logger)
);

export default function configureStore(initialState?: counterStateType) {
export default (initialState: ?counterStateType) => {
// Redux Configuration
const middleware = [];
const enhancers = [];

// Thunk Middleware
middleware.push(thunk);

// Logging Middleware
const logger = createLogger({
level: 'info',
collapsed: true
});
middleware.push(logger);

// Router Middleware
const router = routerMiddleware(hashHistory);
middleware.push(router);

// Redux DevTools Configuration
const actionCreators = {
...counterActions,
push,
};
// If Redux DevTools Extension is installed use it, otherwise use Redux compose
/* eslint-disable no-underscore-dangle */
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// Options: http://zalmoxisus.github.io/redux-devtools-extension/API/Arguments.html
actionCreators,
})
: compose;
/* eslint-enable no-underscore-dangle */

// Apply Middleware & Compose Enhancers
enhancers.push(applyMiddleware(...middleware));
const enhancer = composeEnhancers(...enhancers);

// Create Store
const store = createStore(rootReducer, initialState, enhancer);

if (module.hot) {
Expand All @@ -43,4 +55,4 @@ export default function configureStore(initialState?: counterStateType) {
}

return store;
}
};

0 comments on commit 9f281c6

Please sign in to comment.