5

I've been trying to use redux-perist to save my redux-state to AsyncStorage. Although I keep getting an error:

_this.store.getState is not a function

I'm not sure why this is happening?

Here is my setup:

configureStore.js:

import {AsyncStorage,} from 'react-native';
import { createStore, applyMiddleware, compose, combineReducers, } from 'redux';
import reduxThunkMiddleware from 'redux-thunk';
import Reactotron from 'reactotron';
import * as reducers from './modules';
import devTools from 'remote-redux-devtools';
import {persistStore, autoRehydrate} from 'redux-persist'


Reactotron.connect({
  enabled: __DEV__,
});

const enhancer = compose(
  autoRehydrate(),
  applyMiddleware(
    reduxThunkMiddleware,
    Reactotron.reduxMiddleware,
  ),
  devTools()
);

export default function configureStore(initialState) {
  const store = createStore(
    combineReducers({
      ...reducers,
    }),
    initialState,
    enhancer,
  );
  Reactotron.addReduxStore(store, {storage: AsyncStorage});
  return store;
}

App.js:

Here is where I connect my store, to my <provider>:

import React from 'react';
import {AsyncStorage} from 'react-native';
import { Provider, connect } from 'react-redux';
import { Router } from 'react-native-router-flux';
import routes from '@routes/app';
import createStore from './redux/create'; // Returns store object from the above configureStore.js!
import {persistStore} from 'redux-persist'
const RouterWithRedux = connect()(Router);

const store = persistStore(createStore(), {storage: AsyncStorage}); // Not working??
const Kernel = () => (
  <Provider store={store}>
    <RouterWithRedux scenes={routes} />
  </Provider>
);

export default Kernel;
1
  • 1
    persistStorage takes a callback which is triggered when it's done, I'm not sure, but perhaps that might be the problem. Here is a working example: pastebin.com/EKXeiYyg Commented May 5, 2016 at 9:12

1 Answer 1

5
const RouterWithRedux = connect()(Router);
const store = createStore();
const persistor = persistStore(store, {storage: AsyncStorage}); // Not working??
const Kernel = () => (
  <Provider store={store} persistor={persistor}>
    <RouterWithRedux scenes={routes} />
  </Provider>
);

The problem was I had to pass down a persistor field as well as the store field.

After adding in the persistor field, my store was being persisted into AsyncStorage

EDIT:

This worked at the time - It has occured to me that this not the correct solution to the problem. But I'm still getting responses that it still works, if someone could provide another answer for everyone else, that'd be great.

Sign up to request clarification or add additional context in comments.

6 Comments

why is this not a recommended way for react-native in redux-persist docs? I was getting "Cannot read property 'subscribe' of undefined" error, this ans fixed the problem. thanks!
I'm actually not sure @Preetam but it did fix it for me
How does this fix the problem? I don't even see a property persistor on the Provider component. github.com/reactjs/react-redux/blob/master/src/components/…
Wow, it works, but why persistor props on Provider when it is not a property on Provider component?
Provider.propTypes = { store: storeShape.isRequired, children: PropTypes.element.isRequired }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.