Skip to content

Instantly share code, notes, and snippets.

@dantenovski
Created July 29, 2018 22:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dantenovski/d53e7340203a585b1c2135ef1ef790fc to your computer and use it in GitHub Desktop.
Save dantenovski/d53e7340203a585b1c2135ef1ef790fc to your computer and use it in GitHub Desktop.
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;
@Diko99
Copy link

Diko99 commented Dec 31, 2021

I dont understand how to calling and use function reHydrateStore ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment