Skip to content

Instantly share code, notes, and snippets.

@rt2zz
Last active March 19, 2016 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rt2zz/6f737b96e4dfdd11f1b8 to your computer and use it in GitHub Desktop.
Save rt2zz/6f737b96e4dfdd11f1b8 to your computer and use it in GitHub Desktop.
import stringify from 'json-stringify-safe'
// use a special rehydrating reducer
const rehydratingReducer = (state, action) => {
if (action.type === 'REHYDRATE') return action.payload
else return reducer(state, action)
}
// subscribe to store and serialize safely
// should either debounce or selectively run this method as it is expensive
store.subscribe(() => {
console.log(stringify(store.getState()))
})
// sometime later dispatch a rehydrate action with the entire state to replace
store.dispatch({type: 'REHYDRATE', payload: JSON.parse(oneState)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment