导航UI卡住时与redux-persist反应

时间:2018-12-05 22:45:29

标签: reactjs react-native redux redux-saga redux-persist

当我浏览该应用程序时,尽管URL发生了变化,但UI仍被卡住。

我想将redux-persist集成到当前应用程序中,但最终使我陷入一个奇怪的错误。

注意:在创建商店时,我还使用redux-saga作为中间件。

store.js

import { createStore, applyMiddleware, compose } from 'redux'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web and AsyncStorage for react-native

import createSagaMiddleware from 'redux-saga'
import rootReducer from "../reducers/index";
import rootSaga from '../sagas/index'

const persistConfig = {
  key: 'root',
  storage,
}

const persistedReducer = persistReducer(persistConfig, rootReducer)

const sagaMiddleware = createSagaMiddleware()

const middleware = applyMiddleware(sagaMiddleware)

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose

const store = createStore(
  persistedReducer,
  {},
  composeEnhancers(middleware)
)

export const persistor = persistStore(store)

sagaMiddleware.run(rootSaga)

export default store

window.store = store

当我在Persist Gate组件中发表评论时,导航将按预期工作。

index.js

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { BrowserRouter as Router } from "react-router-dom";
import { Provider } from 'react-redux'
import registerServiceWorker from "./js/registerServiceWorker";
import { PersistGate } from 'redux-persist/integration/react'
import store, { persistor } from './js/store';

ReactDOM.render(
  <Router>
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <App />
      </PersistGate>
    </Provider>
  </Router >,
  document.getElementById("root")
);
registerServiceWorker();

我希望我能说清楚!

1 个答案:

答案 0 :(得分:0)

尝试用Router包装PersistGate。这些更高阶组件的顺序对于React Router很重要。现在的方式,更改网址不会触发重新渲染,因此交换订单应该可以解决问题。