0

我实现了 redux-persist 到 connected-react-router。起初,一切似乎都奏效了。Redux 存储连接到本地存储并保持状态。但是我意识到,当我从电子邮件中打开链接(http://localhost:9080/reset?token=###)以重置密码时,网站只是停留在当前位置。当我尝试手动更改 url 时,它不起作用。

链接有效。当我在其他浏览器中打开链接时,它可以工作。Url 将在没有 redux-persist 设置的情况下手动更改。

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,
);

我已经尝试过。

我已经尝试添加如下所示的“connectRouter”。它似乎没有改变任何东西。我应该在商店中设置“connectRouter”吗?

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

当我使用'rootReducer'而不是'rootReducer(history)'时,历史将与'connectRouter(history)(persistedReducer)'重复。它将中断并显示错误消息:无法读取未定义的属性“位置”。

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

谢谢 !!

4

1 回答 1

0

使用黑名单不坚持或白名单坚持!

const persistConfig = { key: 'root', storage, blacklist: ['router'], };

于 2019-07-20T09:53:53.393 回答