Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_this.store.getState is not a function #85

Closed
jamesone opened this issue May 5, 2016 · 4 comments
Closed

_this.store.getState is not a function #85

jamesone opened this issue May 5, 2016 · 4 comments

Comments

@jamesone
Copy link

jamesone commented May 5, 2016

I'm having trouble setting up redux-persist. I keep getting an error - _this.store.getState is not a function

I have no idea why it's not working, I also found that their weren't any examples! If I get this running I'll try put up an example for other users.

This is my configureStore.js file

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;
}

And here is where I'm passing the store down to the <provider> component

import createStore from './redux/configureStore';
import {persistStore} from 'redux-persist'
const store = persistStore(createStore(), {storage: AsyncStorage});


const RouterWithRedux = connect()(Router);
const Kernel = () => (
  <Provider store={store}>

Also a question I have:

Is it possible for me to only persist an array of reducers? I've got about 10 reducers, and I'd only like to persist 2 or 3 of them to AsyncStorage?

@rt2zz
Copy link
Owner

rt2zz commented May 5, 2016

  1. the return value from persistStore is persistor not store. So your code should look more like:
import createStore from './redux/configureStore'
import {persistStore} from 'redux-persist'
const store = createStore()
const persistor =  persistStore(store, {storage: AsyncStorage})
  1. You can pass in a whitelist of reducer keys like so:
    const persistor = persistStore(store, {storage: AsyncStorage, whitelist: ['reducerA', 'reducerB']})

@jamesone
Copy link
Author

jamesone commented May 5, 2016

Great! Would I pass down the store as well as the persistor object to my provider? So basically the persistor object would only persist the keys you put in the whitelist array? @rt2zz

 <Provider store={store} persistor={persistor}>

@jamesone
Copy link
Author

jamesone commented May 5, 2016

One more thing, Is it possible to pass a persisted store down to my child the <Provider component?

The following store={store} is just parsing down my default reducer, instead of the persisted one?

<Provider persistor={persistor} store={store}>
    <Routes store={store} /> // This will parse down my default store
  </Provider>

@rt2zz
Copy link
Owner

rt2zz commented May 5, 2016

no need to pass persistor to provider. I would recommend reading up more on how react-redux works. Basically if you want access to state you use should using connect. redux-persist will not affect anything about react-redux.

@rt2zz rt2zz closed this as completed May 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants