question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redux-persist Implementation in React / connected-react-router

See original GitHub issue

I implemented redux-persist to react v16 / connected-react-router. At first, everything seemed to work. Redux store is connected to local storage and it persists states. But I realized when I open the link (http://localhost:9080/reset?token=###) from my email to reset the password, the website just sticks to the current location. When I tries to change url manually, it does not work.

The link is valid. When i open the link in other browsers, it works. Url would be manually changed without redux-persist setting.

store.js

import { applyMiddleware, createStore } from 'redux';
import thunkMiddleware from 'redux-thunk';
import { createLogger } from 'redux-logger';
import { composeWithDevTools } from 'redux-devtools-extension';
import { routerMiddleware, connectRouter } from 'connected-react-router';
import createBrowserHistory from 'history/createBrowserHistory';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import rootReducer from './src/reducers';

export const history = createBrowserHistory();
const middlewares = [
  routerMiddleware(history), 
  thunkMiddleware,
  createLogger({
    predicate: () => process.env.NODE_ENV === 'development',
    collapsed: true,
  }),
];
const enhancers = [applyMiddleware(...middlewares)];
const persistConfig = {
  key: 'root',
  storage,
};
const persistedReducer = persistReducer(
  persistConfig,
  rootReducer(history), 
);

export default preloadedState => {
  const store = createStore(
    persistedReducer,
    preloadedState,
    composeWithDevTools(...enhancers),
  );
  const persistor = persistStore(store);

  return { store, persistor };
};

index.js

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { ConnectedRouter as Router } from 'connected-react-router';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import { PersistGate } from 'redux-persist/lib/integration/react';
import configureStore, { history } from './store';
import Routes from './routes';
import Loading from './src/shared/loading';

const theme = createMuiTheme({
 ...
});
const { store, persistor } = configureStore();
const root = document.createElement('div');
document.body.appendChild(root);

render(
  <Provider store={store}>
    <PersistGate loading={<Loading />} persistor={persistor}>
      <MuiThemeProvider theme={theme}>
        <Router history={history}>
          <Routes />
        </Router>
      </MuiThemeProvider>
    </PersistGate>
  </Provider>,
  root,
);

What I have tried.

And I have tried to add ‘connectRouter’ like below. It doesn’t seem to change anything. Am I supposed to set ‘connectRouter’ in store ?

const store = createStore(
  // persistedReducer,
  connectRouter(history)(persistedReducer),
  composeWithDevTools(enhancers),
);

When i use ‘rootReducer’ instead of ‘rootReducer(history)’ as history will be duplicated with ‘connectRouter(history)(persistedReducer)’. It will break with an error message : Cannot read property ‘location’ of undefined.

"connected-react-router": "^6.4.0"
"redux-persist": "^5.10.0"
"react": "^16.8.6"
"react-dom": "^16.8.6"
"redux": "^4.0.0"

Thanks !!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

18reactions
ghostcommented, Jul 20, 2019

Hey, did you try to put the router key to blacklist?

const persistConfig = {
  key: 'root',
  storage,
  blacklist: ['router'],
};
1reaction
sohrobraja3commented, Jul 29, 2019

@mrtrebor I am having the same issue; however, when I add the blacklist to the persistConfig, this prevents any routing throughout the application. @Jin827 Are you still able to navigate to different routes with the blacklist field set?

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-Router with redux-persist
I've tried looking at guides online but none seem to integrate redux-persist with connected-react-router . How would I go about using the ...
Read more >
React Router with Redux: Understanding navigation state
Use React Router to declaratively navigate within your React and Redux applications and maintain state across your app's navigation ...
Read more >
How to Integrate Redux Persist to React Redux Store
We will step by step learn how to configure redux persist in the redux store using react-redux, redux toolkit, and redux persist libraries....
Read more >
Using React Router with Redux
You can use the connected-react-router library (formerly known as react-router-redux ). Their Github Repo details the steps for the integration.
Read more >
Redux Essentials, Part 4: Using Redux Data
The official Redux Essentials tutorial: learn how to work with complex Redux state in React components.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found