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

redux-persist-immutable tries to parse state object as json

Open ghost opened this issue 9 years ago • 2 comments

When trying to configure redux-persist-immutable, it fails with unexpected token o in json at position 1 which indicates to me that something is trying to parse an object as JSON. Tracing the code down, I found that redux-persist-transform-immutable/index.js is expecting JSON when it creates its transformers. I modified their code to transform raw into JSON right before it transforms it, and the error goes away; however, the data does not get autoRehydrated.

Here is how I've implemented redux-persist-immutable, right before I hand the store over to <Provider> I have more reducers, but I've trimmed it down for brevity.

store.js
import { AsyncStorage } from 'react-native';
import { fromJS } from 'immutable';
import { compose, applyMiddleware, createStore } from 'redux';
import { combineReducers } from 'redux-immutable';
import { persistStore, autoRehydrate } from 'redux-persist-immutable';
import createSagaMiddleware from 'redux-saga';
import logger from 'redux-logger';
import sagas from './sagas';

import appReducer from './containers/App/reducer';

export default function configureStore(initialState = {}) {
  // Middleware
  const sagaMiddleware = createSagaMiddleware({
    logger: () => {},
  });
  const middlewares = [
    sagaMiddleware,
    logger(),
  ];
  const enhancers = [
    applyMiddleware(...middlewares),
  ];

  const store = createStore(
    combineReducers({
      global: appReducer,
    }),
    fromJS(initialState),
    compose(...enhancers)
  );

  sagas.map((container) => {
    container.map((sagaGroup) =>{
      sagaMiddleware.run(sagaGroup);
    });
  });

  persistStore(store, { storage: AsyncStorage });

  return store;
}

ghost avatar Feb 23 '17 03:02 ghost

Edit: Appears I should be using redux-persist-transform-immutable rather than redux-persist-immutable.

sheraz-sarwar avatar May 22 '17 15:05 sheraz-sarwar

this library should be able to handle both interchangeably. It may require an is immutable check on the top level and fallback to normal reconciliation. PR welcome!

rt2zz avatar May 22 '17 17:05 rt2zz