elgerlambert / redux-localstorage

Store enhancer that syncs (a subset) of your Redux store state to localstorage.
MIT License
1.32k stars 107 forks source link

Add an example config for Immutable.js to the README.md #49

Open NameFILIP opened 8 years ago

NameFILIP commented 8 years ago

I've spent a couple of hours figuring out how to make it play well with Immutable.js Immutable.js is a quite popular library that's often used together with redux. It would be nice if the documentation contain an example of configuration (can be taken from here: https://github.com/elgerlambert/redux-localstorage/issues/22)

elgerlambert commented 8 years ago

Hi @NameFILIP,

There are a couple of comments in that issue with solutions, could you specify which comment in particular was helpful to you? Also, which version of redux-localstorage are you using?

In case you're using 1.0.0-x, did you also see #28 / https://github.com/jakelazaroff/redux-localstorage-immutable.

NameFILIP commented 8 years ago

The last one, from @kretz (https://github.com/elgerlambert/redux-localstorage/issues/22#issuecomment-158659363). It works fine with v0.4.1

I've looked at the 1.0.0-x version, but I currently just need the simplest solution

josephearl commented 7 years ago

The following should work using redux-localstorage 1.0.0 and redux-localstorage-immutable:

import {compose, createStore} from 'redux';
import rootReducer from './reducers';

import persistState, {mergePersistedState} from 'redux-localstorage';
import adapter from 'redux-localstorage/lib/adapters/localStorage';
import filter from 'redux-localstorage-filter';

import { serialize, deserialize } from 'redux-localstorage-immutable';

const reducer = compose(
  // apply deserialize from redux-localstorage-immutable
  mergePersistedState(deserialize)
)(rootReducer);

const storage = compose(
  // apply serialize from redux-localstorage-immutable
  serialize,
  filter('nested.key')
)(adapter(window.localStorage));

const enhancer = compose(
  /* applyMiddleware(...middlewares) */
  persistState(storage, 'my-storage-key')
);

const store = createStore(reducer /*, [initialState]*/, enhancer);
NameFILIP commented 7 years ago

@JosephEarl Can you create a PR to the 1.0 branch with your example?

josephearl commented 7 years ago

@NameFILIP done!

steveinatorx commented 7 years ago

i also got hung up on combineReducers which i had been importing from the base redux module - replacing with combineReducers from 'redux-immutable' fixed my serialization/deserialization exceptions: https://github.com/gajus/redux-immutable