2
votes

Hi I am trying to setup redux-persist with react-redux, but I cant get it to work. I get the following error:

TypeError: _store2.default is not a function [Learn More] index.js:12:29

How I have the setup right now:

store.js

import {applyMiddleware, createStore} from 'redux';
import {persistStore,persistCombineReducers} from 'redux-persist';
import storage from 'redux-persist/es/storage' // default: localStorage if web, AsyncStorage if react-native

import { logger } from 'redux-logger';
import thunk from 'redux-thunk';
import promise from 'redux-promise-middleware';
import reducer from './reducers'

const middleware = applyMiddleware(promise(), thunk, logger);

const config = {
  key: 'root',
  storage,
};

const reducers = persistCombineReducers(config, {reducer});

export const configureStore = () => {
  const store = createStore(reducers, middleware);
  const persistor = persistStore(store);
  return { persistor, store };
};

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter} from 'react-router-dom';
import {Provider} from 'react-redux';
import Bootstrap from 'bootstrap/dist/css/bootstrap.css';
import './css/app.css';
import App from './containers/App';

import { PersistGate } from 'redux-persist/es/integration/react'
import configureStore from './store';
const { persistor, store } = configureStore()


ReactDOM.render(
  <Provider store={store} >
    <PersistGate persistor={persistor}>
    <BrowserRouter>
      <App/>
    </BrowserRouter>
  </PersistGate>
  </Provider>,
  document.getElementById('root')
);

UPDATE 1

Based on @azium's response now I get:

The above error occurred in the component: in Connect(App) (created by Route) in Route (created by withRouter(Connect(App))) in withRouter(Connect(App)) in Router (created by BrowserRouter) in BrowserRouter in PersistGate in Provider

When calling it like so from App.js:

@withRouter
@connect((store) => {
  return {
    isAuthenticated: store.auth.isAuthenticated,
  };
})
2

2 Answers

3
votes

If you want to use the default export you need to change:

export const configureStore = () => {
  const store = createStore(reducers, middleware);
  const persistor = persistStore(store);
  return { persistor, store };
};

to:

export default () => {
  const store = createStore(reducers, middleware);
  const persistor = persistStore(store);
  return { persistor, store };
};

or:

const configureStore = () => {
  const store = createStore(reducers, middleware);
  const persistor = persistStore(store);
  return { persistor, store };
};

export default configureStore;

or if you don't want to use default export change:

import configureStore from './store';

to:

import { configureStore } from './store';
0
votes

So after a bit of thinkering and community help I managed to narrow down the issue to my reducer declaration. I was declaring reducer in index.js with combineReducers whilst redux-persist says it shouldn't be.

Final index.js code:

import user from './userReducer'
import auth from './authReducer'

export default ({
  user, auth
})