n0f3
4/14/2017 - 6:56 AM

Learn redux snippets #javascript

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);
}