configureStore.js 1.3 KB
import thunk from 'redux-thunk';
// import logger from 'redux-logger'
import rootReducer from './reducers';
import {
  applyMiddleware,
  compose,
  createStore
} from 'redux';

export default function configureStore(initialState) {
  let createStoreWithMiddleware;
  const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
  const middleware = applyMiddleware(thunk
    // , logger
  );

  if (__DEBUG__) {
    if (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
      createStoreWithMiddleware = composeEnhancers(
        middleware
      );
    }
  } else {
    createStoreWithMiddleware = compose(middleware);
  }

  const store = createStoreWithMiddleware(createStore)(
    rootReducer, initialState,
    // dev-tools的另外一种调用方法,前提是在自己的浏览器中安装了扩展 redux-devtools
    //  1、 添加下边语句
    /*  2、将该文件的17行内容 'require('./../containers/devtools/DevTools').instrument()' 注释掉
        原因是不允许调用生成调试工具两次
     */


    // window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() 

  );
  if (module.hot) {
    module.hot.accept('./reducers', () => {
      const nextRootReducer = require('./reducers');
      store.replaceReducer(nextRootReducer);
    });
  }
  return store;
}