Redux永久存储不存储任何数据以供离线使用

时间:2018-09-21 08:13:39

标签: react-native redux react-redux redux-thunk redux-persist

  

我正在使用redux,redux-persist和中间件redux-thunk来   拿杰森       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 };
};

1 个答案:

答案 0 :(得分: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;