4

I have been trying to setup redux-persist 5.9.1 with reactboilerplate 3.4.0 framework.

The error I receive seems to related to redux-immutable and I am unable to figure out right configuration.

Here is what I have done so far:

1. Install NPM

npm i -S redux-persist redux-persist-transform-immutable

package.json "redux-persist": "^5.9.1", "redux-persist-transform-immutable": "^5.0.0",

2. Setup Redux Persist in store.js

//store.js
import .... (other usual stuff)
import { persistStore, persistReducer  } from 'redux-persist';
import storageSession from 'redux-persist/lib/storage/session';
import immutableTransform from 'redux-persist-transform-immutable';

const persistConfig = {
  transforms: [immutableTransform()],
  key: 'root',
  storage: storageSession,
}

const rootReducers = createReducer();

// Using persistReducer not persistCombineReducer because the rootReducer is already returned by combinedReducer from redux-immutable.
const persistedReducer = persistReducer (persistConfig, rootReducers)

export default function configureStore (initialState = {}, history) {
   // other usual stuffs ... 

   // I modified how store is created using persistedReducer

   const store = createStore(
      persistedReducer, // this line used to use createReducer() method
      fromJS(initialState),
      composeEnhancers(...enhancers),
   );

   const persistor = persistStore(store);

   return { persistor, store };

   // Please note, I have commented out hot reloading of reducers for now.
}

3. No change in reducers.js

4. Update App.js

import 'babel-polyfill';
import React from 'react';

// Added below
import { PersistGate } from 'redux-persist/es/integration/react';

// other usual setup

// Line below used to define just store but now we are defining persistor and store
const { persistor, store } = configureStore(initialState, browserHistory);

// Finally, update the render method:

const render = () => {
  ReactDOM.render(
    <Provider store={store}>
      <PersistGate persistor={persistor}>
        <Router
          history={history}
          routes={rootRoute}
          render={
            applyRouterMiddleware(useScroll())
          }
        />
      </PersistGate>
    </Provider>,
    document.getElementById('app')
  );
};

And still no luck:

Error:

I think I do not have immutable maps configured right. Any help?

redux-persist error

absqueued
  • 3,013
  • 3
  • 21
  • 43

1 Answers1

3

The way you doing is correct as documentation.

The problem is in here:

const rootReducers = createReducer();

// Using persistReducer not persistCombineReducer because the rootReducer is already returned by combinedReducer from redux-immutable.
const persistedReducer = persistReducer (persistConfig, rootReducers)

This const rootReducers = createReducer(); should not call like that, it will trigger the function. You should put like const rootReducers = createReducer; or better call like this:

const persistedReducer = persistReducer (persistConfig, createReducer)

Please see documentation, not call rootReducer for trigger function but pass it as variable.

hendrathings
  • 3,720
  • 16
  • 30
  • 2
    Did that, and now a diff problem. state.get says its not a function. – absqueued Mar 12 '18 at 18:19
  • you get the error `state.get` because you not set `PersistGate`. – hendrathings Mar 13 '18 at 14:26
  • I think, when you change to redux persist, you need update everything `state` inside of it, like container or component. this is not easy way. check https://github.com/hendrathings/react-boilerplate-redux-persist-sample. find `app/app.js`, in that file find `// todo: 1. comment this ` for more play render – hendrathings Mar 13 '18 at 14:38
  • @hendrathings i cloned your repo. it shows the same warning on OP and this error `ReactCompositeComponent.js:742 Uncaught (in promise) TypeError: Cannot read property '_currentElement' of null` – Theo Jul 14 '18 at 14:05
  • @hendrathings I'm also getting the same state.get, even after I've set PersistGate – Sidharth Aug 19 '18 at 21:44
  • @ShekharK.Sharma did you manage to fix the state.get is not a function? – Sidharth Aug 19 '18 at 21:48
  • @Sidharth just in case someone is searching for this: A possible issue might be that you had a `stateReconciler` in your `persistConfig`, like `autoMergeLevel2`. This might then lead to a situation where your reducer expects an immutable object but receives a plain js object, which doesn't have a `get` function. – Ole Spaarmann Apr 17 '20 at 21:22