1

我正在使用 redux、redux-persist 和中间件 redux-thunk 来获取 Json API。我的问题是当 wifi 关闭时我不能再使用它了。存储不能很好地离线使用。

 Here is my codes.

 Store.js 
 -------------
import { createStore, applyMiddleware, compose } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/es/storage';
import thunk from 'redux-thunk';
import rootReducer from './RootReducers';

const middleware = [thunk];
const persistConfig = {
  key: 'root',
  storage
};

const persistedReducer = persistReducer(persistConfig, rootReducer);


const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const enhancers = composeEnhancers(
  applyMiddleware(...middleware),
);

export default () => {
  const store = createStore(persistedReducer, enhancers)
    const persistor = persistStore(store);
  return { store, persistor: persistor };
};
4

1 回答 1

1

也许您忘记从configureStore.js.

在您的应用入口点中调用它。我认为在这一点上,redux-persist库的文档记录很差,因为您可以在问题选项卡中找到最多的解决方案。

这对我有用:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

// store
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';

import configureStore from './configureStore.js';

// call your store config
const { persistor, store } = configureStore();

class App extends Component {
  render() {
    return (
      <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
          <div>
             App
          </div>
        </PersistGate>
      </Provider>
    );
  }
}

export default App;

于 2018-09-21T08:58:59.080 回答