elgerlambert / redux-localstorage

Store enhancer that syncs (a subset) of your Redux store state to localstorage.
MIT License
1.32k stars 107 forks source link

Can't rehydrate state - stored state won't merge with initial state. #25

Closed localhosted closed 8 years ago

localhosted commented 8 years ago

This is the code I am using to try to set up my React + Redux app:

let finalCreateStore;

const storage = compose(
    filter('redux')
)(adapter(window.localStorage));

if (__DEV_TOOLS__) {
  finalCreateStore = compose(
    applyMiddleware(logger, thunk),
    reduxReactRouter({routes, createHistory}),
    devTools(),
    persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/))
  )(createStore);
} else {
  finalCreateStore = compose(
    applyMiddleware(logger, thunk),
    reduxReactRouter({routes, createHistory}),
    persistStateLocalStorage(storage, 'redux')(createStore);
}

let finalRootReducer = compose(
    mergePersistedState((initialState, persistedState) => {
      return initialState.redux.mergeDeep(Immutable.fromJS(persistedState.redux));
    })
)(rootReducer);

const configureStore = () => {
  const store = finalCreateStore(finalRootReducer);

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../reducers', () => {
      const nextRootReducer = require('../reducers/index');
      store.replaceReducer(nextRootReducer);
    });
  }

return store;
};

But still the state won't update correctly with the data from localstorage. Even if setting the data to an empty object, it still uses the initial data:

mergePersistedState((initialState, persistedState) => {
    return {};
})
elgerlambert commented 8 years ago

Hi @localhosted, did you end up solving the issue? I'm afraid I'm unable to pinpoint a problem looking at the code you shared.

The only thing that strikes me as a little odd is having a "redux" key inside of your store state (could mean you're nesting your store state unnecessarily), but that shouldn't really cause an issue.

localhosted commented 8 years ago

I'm not sure either. I got it working after "resetting" the whole project.