Skip to content

Instantly share code, notes, and snippets.

@n0f3
Last active February 20, 2018 22:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save n0f3/3a7a969f5d8227ba2bb601d1f3dcaf16 to your computer and use it in GitHub Desktop.
Learn redux snippets #javascript
// 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