Skip to content

Instantly share code, notes, and snippets.

@possibilities
Last active July 19, 2016 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save possibilities/5b5e9e028df89c6a9ec6 to your computer and use it in GitHub Desktop.
Save possibilities/5b5e9e028df89c6a9ec6 to your computer and use it in GitHub Desktop.
import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import reducer from '../reducers'
const helloWorldEnhancer = () => {
return nextCreateStore => (reducer, initialState) => {
const store = nextCreateStore(reducer, initialState)
const originalDispatch = store.dispatch
const dispatch = (...args) => {
console.info('dispatch: start', ...args)
const result = originalDispatch(...args)
console.info('dispatch: end', ...args)
return result
}
return {
...store,
dispatch
}
}
}
const finalCreateStore = compose(
applyMiddleware(thunk),
helloWorldEnhancer()
)(createStore)
export default function configureStore(initialState) {
const store = finalCreateStore(reducer, initialState)
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
const nextReducer = require('../reducers')
store.replaceReducer(nextReducer)
})
}
return store
}
@possibilities
Copy link
Author

I tentatively disagree with myself that this is a hello world because I'm not sure customizing dispatch is the most obvious case for an enhancer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment