How to persist redux store to local storage rather than async storage in react native

370 Views Asked by At

I am using redux-persist to persist state of my app. By default it persist state inside AsyncStorage but I want it to be in localStorage. There is nothing about this in official docs as far as react-native is concerned. This is my code till now:

import { persistStore, persistCombineReducers } from 'redux-persist';
import storage from 'redux-persist/es/storage';

const config = {
    key: 'root',
    storage,
    debug: true
  }

export const ConfigureStore = () => {
    const store = createStore(
        persistCombineReducers(config,{
            dishes,
            comments,
            promotions,
            leaders,
            favorite
        }),
        applyMiddleware(thunk)
    );

    const persistor = persistStore(store)

    return {persistor, store};
}

Can I do something like this:

import storage from 'redux-persist/es/localStorage';
...
storage: storage

Please help me on the topic if anyone has ever used it before. Thank You!

1

There are 1 best solutions below

0
On

One thing i noticed is that you import says

import storage from 'redux-persist/es/storage'; But in the documentation is used import storage from 'redux-persist/lib/storage'

Otherwise you can use this approach like in the documentation.

import { createStore } from 'redux'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web

import rootReducer from './reducers'

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

const persistedReducer = persistReducer(persistConfig, rootReducer)

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

import { PersistGate } from 'redux-persist/integration/react'

// ... normal setup, create store and persistor, import components etc.

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