Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rehydrating Redux store when using systemjs hot reloader
import {createStore} from 'redux';
import reducer from './reducers/index.js'
import { rehydrate, rehydratingStore } from './utils/rehydratingStore.js';
const store = rehydratingStore()(createStore)(reducer);
export function __reload(deletedModule){
const prevState = deletedModule.getState();
debug('Reloaded. rehydrate with state: ', prevState.sketcher.objectsById);
store.dispatch(rehydrate(prevState));
}
export function getState() {
return store.getState();
}
const REHYDRATE = 'REHYDRATE';
export function rehydrate(state) {
return { type: REHYDRATE, state };
};
export function rehydratingStore (config = {}) {
return (next) => (reducer, initialState) => {
const rehydrationReducer = createRehydrationReducer(reducer);
const store = next(rehydrationReducer, initialState);
const dispatch = store.dispatch;
return {
...store,
dispatch
}
}
function createRehydrationReducer (reducer) {
return (state, action) => {
if (action.type === REHYDRATE) {
let state = action.state;
state = reducer(state, action)
return state
} else {
return reducer(state, action);
}
}
}
}
@johnjelinek

This comment has been minimized.

Copy link

@johnjelinek johnjelinek commented Dec 21, 2015

hrmm .. I'm having trouble with this. My store seems to be reseting state on every action.

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