无法读取未定义的属性“订阅”-Redux Persist

时间:2018-07-25 00:28:44

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

我得到的更具体的错误是:警告:道具类型失败:道具storeProvider中标记为必需,但其值为undefined

store.js

const initialState = {};

const storage = createSensitiveStorage({
  keychainService: "myKeychain",
  sharedPreferencesName: "mySharedPrefs"
});

const config = {
  key: "root",
  storage,
};

const middleware = [thunk];

const reducer = persistCombineReducers(config, rootReducer);

export default () => {
  let store = createStore(
    reducer,
    initialState,
    compose(applyMiddleware(...middleware))
  );
  let persistor = persistStore(store);

  return { store, persistor }
}

reducer的index.js:

export default combineReducers({
    profile: profileReducer,
    auth: authReducer,
    photoSlider,
    trip: tripReducer,
    moment: momentReducer
})

App.js:

import {store, persistor} from './src/store';

<Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
          <Layout />
        </PersistGate>
      </Provider>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:5)

store.js中,您不导出storepersistor,而是导出了一个函数,该函数返回带有storepersistor的对象。因此,您在import中的App.js语句实际上导入了两个不存在的命名导入

您可以这样获取实例:

import factory from './src/store';

const {store, persistor} = factory();

<Provider store={store}>
  <PersistGate loading={null} persistor={persistor}>
     <Layout />
  </PersistGate>
</Provider>