Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #49 - Add example of integrating with immutable.js using redux-localstorage-immutable #57

Open
wants to merge 1 commit into
base: 1.0-breaking-changes
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,40 @@ const enhancer = compose(
```
Check out the wiki for a [list of available storage enhancers](https://github.com/elgerlambert/redux-localstorage/wiki) and don't forget to add your own if you publish any!

## Immutable

By default redux-localstorage assumes your state is a plain JS object. If you are using [immutable.js](https://facebook.github.io/immutable-js/) to manage your state you can apply the merge and store enhancer functions provided by [redux-localstorage-immutable](https://www.npmjs.com/package/redux-localstorage-immutable) to enable `redux-localstorage` to understand your values.

To use `redux-localstorage-immutable` with `redux-localstorage` pass the `deserialize` merge function to `mergePersistedState` and apply the `serialize` store enhancer to `compose` before any other store enhancers such as `filter`.

```
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);
```

## License
MIT