redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

PersistGate causing Uncaught TypeError: Super expression must either be null or a function, not undefined.

Open lesansley opened this issue 6 years ago • 8 comments

When I include import { PersistGate } from 'redux-persist/integration/react'; into my app.js file it throws an exception Uncaught TypeError. I am not sure how to proceed.

My code is as follows: App.js

import 'whatwg-fetch';
import React, { Component } from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import reduxPromise from 'redux-promise-middleware';
import { BrowserRouter, browserHistory } from 'react-router-dom';
import { PersistGate } from 'redux-persist/integration/react';

import { store, persistor } from './config/reduxStore';
import { Header, Main } from './components';
import reducers from './reducers';

const createStoreWithMiddleware = applyMiddleware(
  reduxPromise()
)(createStore);

class App extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <Provider store={store}>
        <PersistGate  loading={<div />} persistor={persistor}>
          <BrowserRouter history={browserHistory}>
            <Main />
          </BrowserRouter>
        </PersistGate>
      </Provider>
    );
  }
}

render(<App />, document.getElementById('app'));

reduxStore.js

import { applyMiddleware, createStore } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // defaults to localStorage for web and AsyncStorage for react-native
import reduxPromise from 'redux-promise-middleware';

import rootReducer from '../reducers';

const middleware = applyMiddleware(reduxPromise());

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

const persistedReducer = persistReducer(persistConfig, rootReducer);

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

lesansley avatar Apr 11 '18 14:04 lesansley

Same problem here.

fsalata avatar Apr 23 '18 14:04 fsalata

Same problem, any solution found?

andela-mallan avatar Jun 01 '18 11:06 andela-mallan

I had to change to use redux-saga.

lesansley avatar Jun 02 '18 12:06 lesansley

You should be able to take the constructor out and just render it. There typically won't be any props passed into the initial component that you've registered.

vincentvella avatar Jun 06 '18 14:06 vincentvella

To anyone who is looking for a fix, the problem is most likely your React version. PersistGate uses React.PureComponent, only available in React v15.3+. See https://stackoverflow.com/questions/51317610/can-not-import-persistgate-from-redux-persist-super-expression-must-either-be/51317754#51317754

Upgrade your React to v15.3+ to fix the issue.

Li357 avatar Jul 13 '18 04:07 Li357

my react version in 18+ but I am getting same issue

rakibhssn avatar Sep 07 '23 15:09 rakibhssn

my react version in 18+ but I am getting same issue

Unsure if you resolved this, and for anyone else who finds this:

Your provider component needs to have "use client" set else the error will happen. The PersistGate component runs as a client component.

byronrode avatar Jan 23 '24 12:01 byronrode

Thanks @byronrode, this solved the issue in my case

EmmanuelVictor62 avatar Jun 23 '24 15:06 EmmanuelVictor62