I'm trying to setup redux, redux-thunk, redux-persist in a React Native app, What am I doing wrong?

configStore.tsx

import {createStore} from 'redux';
import {rootReducer} from './reducers';
import {persistStore, persistReducer} from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import AsyncStorage from '@react-native-community/async-storage';
import {applyMiddleware} from 'redux';
import thunk from 'redux-thunk';

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

const persistedReducer = persistReducer(persistConfig, rootReducer);

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

App.tsx

import React, {useState} from 'react';
import Drawer from './PreApp';
import {View, Text, Button, StyleSheet} from 'react-native';
import {Provider} from 'react-redux';
import store from './redux/configureStore';
import {PersistGate} from 'redux-persist/integration/react';
import persistor from './redux/configureStore';

const App = () => {
  return (
    <Provider store={store}>
      <Drawer />
    </Provider>
  );
};
export default App;

This is the error message I get: enter image description here

1

There are 1 answers

0
fayeed On

You are exporting a function from the configureStore you will first need to call it to get the store & persistor which you can later passed to the Provider & PersistGate.

import React, {useState} from 'react';
import Drawer from './PreApp';
import {View, Text, Button, StyleSheet} from 'react-native';
import {Provider} from 'react-redux';
import createStore from './redux/configureStore'; // imported the default function
import {PersistGate} from 'redux-persist/integration/react';

const {store, persistor} = createStore(); // destructed the result to get the store & persistor

const App = () => {
  return (
    <Provider store={store}>
      {/*Wrap the main component PersistGate*/}
      <PersistGate loading={<Text>Loading...</Text>} persistor={persistor} > 
        <Drawer />
      </PersistGate>
    </Provider>
  );
};
export default App;