redux-persist:undefined不是对象(正在评估store.getState)

时间:2019-03-03 18:00:23

标签: react-native redux redux-persist

这是我第一次使用Redux和Redux Persist。 当我尝试运行我的应用程序时(在index.js第20行中),我收到此错误:

  

TypeError:未定义不是对象(正在评估“ store.getState”)

index.js:

import React from 'react';
import { AppRegistry } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
import { Provider } from 'react-redux';
import {persistor, store}from './store/configureStore';
import { PersistGate } from 'redux-persist/integration/react';

const RNRedux = () => (
    <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
            <App/>
        </PersistGate>
    </Provider>
)

AppRegistry.registerComponent(appName, () => RNRedux);

App.js:

import {createStackNavigator, createAppContainer} from 'react-navigation';
import Register from './components/Register';
import Home from './components/Home';
import Login from './components/Login';

const AppNavigator = createStackNavigator({
  HomeScreen: {
    screen: Home,
    navigationOptions: {
      title: 'Home',
      header: null
    }
  },
  LoginScreen: {screen: Login},
  RegisterScreen: {
    screen: Register,
    navigationOptions: {
      title: 'Register'
    }
  }
});

const App = createAppContainer(AppNavigator);

export default App;

configureStore.js:

import { createStore, combineReducers } from 'redux';
import userReducer from './reducers/user';
import { persistStore, persistCombineReducers, persistReducer } from 'redux-persist';
import storage from 'redux-persist/es/storage' // default: localStorage if web, AsyncStorage if react-native
import { AsyncStorage } from 'react-native';

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

const rootReducer = persistCombineReducers(persistConfig, {
    user: userReducer
});

const persistedReducer = persistReducer(persistConfig, rootReducer);

export default () => {
    let store = createStore(persistedReducer)
    let persistor = persistStore(store, {storage: AsyncStorage})
    return { store, persistor };
}

2 个答案:

答案 0 :(得分:3)

您正在从configureStore.js导出函数,但尝试在{persistor, store}中导入index.js

您应该改为导入函数并调用它。

例如:

import configureStore from './store/configureStore';
const {store, persistor} = configureStore();

答案 1 :(得分:0)

import thunk from "redux-thunk";
import { persistStore, persistReducer } from "redux-persist";
import { AsyncStorage } from "react-native";
import logger from "redux-logger";

const persistConfig = {
  key: "root",
  storage: AsyncStorage,
  whitelist: ["authReducer"],
};

const persistedReducer = persistReducer(persistConfig, rootReducer);
const middleWares = [thunk, logger];

const store = createStore(persistedReducer, applyMiddleware(...middleWares));
let persistor = persistStore(store);

export default { store, persistor };

我遇到了同样的错误“未定义不是对象(正在评估'store.getState')” 导出后删除默认关键字后,我的问题得到解决

export { store, persistor };