Skip to content

Instantly share code, notes, and snippets.

@jacobp100
Created August 22, 2016 15:19
Show Gist options
  • Save jacobp100/75f895ad1adf6c481acc9e88e4d2b2fa to your computer and use it in GitHub Desktop.
Save jacobp100/75f895ad1adf6c481acc9e88e4d2b2fa to your computer and use it in GitHub Desktop.
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