Created
July 29, 2018 22:57
-
-
Save dantenovski/d53e7340203a585b1c2135ef1ef790fc to your computer and use it in GitHub Desktop.
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
import {createStore, applyMiddleware} from 'redux' | |
import createSagaMiddleware from 'redux-saga'; | |
import decoristReducers from '../reducers/decorist_reducer' | |
import sagas from '../sagas/sagas'; | |
const sagaMiddleware = createSagaMiddleware(); | |
/** | |
* Add all the state in local storage | |
* @param getState | |
* @returns {function(*): function(*=)} | |
*/ | |
const localStorageMiddleware = ({getState}) => { // <--- FOCUS HERE | |
return (next) => (action) => { | |
const result = next(action); | |
localStorage.setItem('applicationState', JSON.stringify( | |
getState() | |
)); | |
return result; | |
}; | |
}; | |
const reHydrateStore = () => { // <-- FOCUS HERE | |
if (localStorage.getItem('applicationState') !== null) { | |
return JSON.parse(localStorage.getItem('applicationState')) // re-hydrate the store | |
} | |
} | |
const store = createStore( | |
decoristReducers, | |
reHydrateStore(),// <-- FOCUS HERE | |
applyMiddleware( | |
sagaMiddleware, | |
localStorageMiddleware,// <-- FOCUS HERE | |
) | |
) | |
sagaMiddleware.run(sagas); | |
export default store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I dont understand how to calling and use function reHydrateStore ?