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

Question: Why does URL not update when used with React-Router after refresh #60

Open newbreedofgeek opened 7 years ago

newbreedofgeek commented 7 years ago

Hello,

This is more of a question rather than a issue.

I am using this module to persist our redux store in a isomorphic react/redux app.

In our server we write out the redux initial state as a global variable (window.__INITIAL_STATE__) on the HTML page, and in our client side we use this to createStore.

Any other idea where I might be making a mistake? My code...

=======================

export default function configureStore(initialState, localStorage = true) {
  /* Middleware
   * Configure this array with the middleware that you want included. thunk
   * is included by default, and react-router-redux's syncHistory is also
   * applied if an `options.history` object was passed to configureStore.
   */
  let middleware = [
    thunk,
    routerMiddleware(browserHistory)
  ];

  // Add universal enhancers here
  let enhancers = [
    DevTools.instrument()
  ];

  // Client-side enhancers and middleware
  if (isBrowser()) {
    if (localStorage) {
      enhancers.push(persistState(persistedStates));
    }
  }

  const enhancer = compose(...[
    applyMiddleware(...middleware),
    ...enhancers
  ]);

  // create store with enhancers, middleware, reducers, and initialState
  const store = createStore(rootReducer, initialState, enhancer);

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

  return store;
}

=======================

const initialState = window.__INITIAL_STATE__;     // this gets set via isomorphic based on initial state of redux
const store = configureStore(initialState); // above configureStore called
const history = syncHistoryWithStore(browserHistory, store);

=======================

As mentioned, the UI state is correct recovered from localstorage but the URL is not updated.

Any help will be appreciated.

Thanks,