Last active
February 20, 2018 22:23
-
-
Save a-metta/3a7a969f5d8227ba2bb601d1f3dcaf16 to your computer and use it in GitHub Desktop.
Learn redux snippets #javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This enables hot loading for reducers | |
if (module.hot) { | |
module.hot.accept('./reducers/', () => { | |
// using require because the import syntax is required to be declared | |
// at the top of the file | |
const nextRootReducer = rquire('./reducers/index').default; | |
store.replaceReducer(nextRootReducer); | |
}) | |
} | |
// Enabling redux dev tools if present | |
const enhancers = compose( | |
window.devToolsExtension ? window.devToolsExtension() : (f) => f | |
); | |
const store = createStore(rootReducer, defaultState, enhancers); | |
// If we want to avoid having to write store.dispatch we can use this | |
// convenience method | |
// bindActionCreators(): Turns an object whose values are action creators, into an object with the same keys, but with every function wrapped into a dispatch call so they may be invoked directly. This is just a convenience method, as you can call store.dispatch(MyActionCreators.doSomething()) yourself just fine. | |
For convenience, you can also pass a single function as the first argument, and get a function in return. | |
function mapDispatchToProps(dispatch) { | |
return bindActionCreators(actionCreators, dispatch); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment