accounts-frontend/packages/app/storeFactory.ts
2020-05-24 02:08:24 +03:00

35 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { createStore, applyMiddleware, compose } from 'redux';
// midleware, который позволяет возвращать из экшенов функции
// это полезно для работы с асинхронными действиями,
// а также дает возможность проверить какие-либо условия перед запуском экшена
// или даже вообще его не запускать в зависимости от условий
import thunk from 'redux-thunk';
import persistState from 'redux-localstorage';
import reducers, { Store } from 'app/reducers';
export default function storeFactory(): Store {
const middlewares = applyMiddleware(thunk);
const persistStateEnhancer = persistState(['accounts', 'user'], {
key: 'redux-storage',
});
let enhancer;
if (process.env.NODE_ENV === 'production') {
enhancer = compose(middlewares, persistStateEnhancer);
} else {
const composeEnhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
enhancer = composeEnhancers(middlewares, persistStateEnhancer);
}
const store = createStore(reducers, {}, enhancer) as Store;
// Hot reload reducers
if (module.hot && typeof module.hot.accept === 'function') {
module.hot.accept('app/reducers', () => store.replaceReducer(require('app/reducers').default));
}
return store;
}