store.getState is not a function Redux-persist

Kadin Zhang

I'm trying to implement Redux-persist on my React Native app. Following the setup docs exactly, I changed my store.js from this:

import { applyMiddleware, createStore } from 'redux';
import * as thunkMiddleware from 'redux-thunk';

import reducers from '../reducers';

let middlewares = [thunkMiddleware.default];
const store = createStore(reducers, applyMiddleware(...middlewares));

export default store;

To this:

import { applyMiddleware, createStore } from 'redux';
import * as thunkMiddleware from 'redux-thunk';
import { persistStore, persistReducer } from 'redux-persist';
import AsyncStorage from '@react-native-community/async-storage';

import reducers from '../reducers';

const persistConfig = {
    key: 'root',
    storage: AsyncStorage,
};
const persistedReducer = persistReducer(persistConfig, reducers);

let middlewares = [thunkMiddleware.default];

export default () => {
    let store = createStore(persistedReducer, applyMiddleware(...middlewares));
    let persistor = persistStore(store);
    return { store, persistor };
};

But now, I'm getting the error TypeError: store.getState is not a function (In 'store.getState()', 'store.getState' is undefined).

Note: I've checked out many questions on stackoverflow with the same store.getState error, but they have very specific issues different from my setup.

Edit: Provider implementation (using RNNv2)

import { Navigation } from 'react-native-navigation';
import { Provider } from 'react-redux';

import store from '../../shared/redux/store';
import { registerScreens } from '../view/screens';
import { initialize } from './navigation';

/**
 * Register screens and components for react native navigation
 */
registerScreens({ store, Provider });


const app = () => {
  Navigation.events().registerAppLaunchedListener(() => {
    initialize();
  });
};

export default app;

Registerscreens:

const registerComponentWithRedux = (redux: any) => (
  name: string,
  component: any,
) => {
  Navigation.registerComponentWithRedux(
    name,
    () => component,
    redux.Provider,
    redux.store,
  );
};

export function registerScreens(redux: any) {
  registerComponentWithRedux(redux)(screen, screen.default);
  ...
}
Eric Hasselbring

The issue is with the export, you are exporting a function that returns store in a key. So if you update the store reference in register screens it will work.

import returnStoreAndPersistor from '../../shared/redux/store';

const {store} = returnStoreAndPersistor();
registerScreens({ store, Provider });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TypeError: store.getState is not a function (redux)

Redux Persist TypeError: store.dispatch is not a function

Redux: Calling store.getState() in a reducer function, is that an anti pattern?

TypeError: store.getState is not a function in react-redux

react-redux : TypeError: store.getState is not a function

TypeError: _this.store.getState is not a function when using connect from Redux

store.getState is not a function

Redux - Undefined is not an object (evaluating 'store.getState')

Is correct to use store.getState() in a redux container?

TypeError: store.getState is not a function. (In 'store.getState()', 'store.getState' is undefined

store.dispatch is not a function - react native redux-persist with redux-thunk middlware

TypeError: getState is not a function when adding middleware to Redux

Uncaught TypeError: store.getState is not a function

React Native TypeError store.getState is not a function

TypeError: store.getState is not a function. (In 'store.getState()', 'store.getState' is undefined how can i resolve this problem?

"Cannot read property 'getState' of undefined" when creating redux mock store

How can I persist nested redux store

Redux-Persist - how to conditionally persist the store in react native

How to store only a part of the redux state using redux-persist

state.getState() is not a function while updating to redux 4

TypeError: getState is not a function in redux when trying to pass token

I am getting this error: TypeError: store.getState is not a function

[RN][Redux-Persist] AutoRehydrate is not a function

Binding a redux store to a function

getState in redux-saga?

Redux getState in action not working

Redux / Angular getState() is undefined

How to getState() in Redux Toolkit

Unable to getState in redux actions