Working with react or react-native and having state management like redux, we have cases when we need to persist our data on refresh, for this task we have redux persist witch is very useful.
So let’s start to add to our react native app, first, we need to install it with yarn or npmyarn add redux-persist
and persistReducer
persistStore
import { persistStore, persistReducer } from 'redux-persist';
The key of configuration is to create a config object with the key ‘root’ , in my case the config is looking like this:
const persistConfig = {
key: 'root',
storage: AsyncStorage,
blacklist: ['user'],
};
After we’ve added a root configuration we need to use what we imported at the beginning:
const persistedReducer = persistReducer(persistConfig, reducer);
And our
function would become:createStore
const store = createStore(persistedReducer, composeEnhancer(applyMiddleware(thunk, ...middleware)));
export const persistor = persistStore(store);
is another discussion, in my case keeps configuration for web dev tools and composeEnhancer
Because we are using react native with redux, we need to include one more configuration in the App.js file, like redux using Provider component as a wrapper, for redux-persist we have PersistGate.

import { AppRegistry } from 'react-native';
import React from 'react';
import { PersistGate } from 'redux-persist/integration/react';
import { Provider } from 'react-redux';
import App from './app/App';
import store, { persistor } from './app/config/store';
import { name as appName } from './app.json';
const AppRedux = () => (
<Provider store={store}>
<PersistGate persistor={persistor} loading={null}>
<App />
</PersistGate>
</Provider>
);
AppRegistry.registerComponent(appName, () => AppRedux);
As we can see Provider is the first wrapper then is PersistGate with persistor and loading. You can add ActivityIndicator if you are using react-native.
1 Comment
wewee