Created
August 22, 2016 15:19
-
-
Save jacobp100/75f895ad1adf6c481acc9e88e4d2b2fa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const middleware = () => store => next => action => { | |
// Get the state before and after the action was performed | |
const previousToken = store.getState().token; | |
next(action); | |
const nextToken = store.getState().token; | |
// Respond to changes | |
if (nextToken !== previousToken) localStorage.setItem('token', nextToken); | |
}; | |
// Get initial state from localStorage | |
const token = localStorage.getItem('token'); | |
const initialState = token | |
? _.set(defaultState, 'token', token) | |
: defaultState; | |
const middlewares = applyMiddleware(middleware()); | |
const store = createStore(reducers, initialState, middlewares); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment