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

Add a configuration example for Immutable.js #56

Open
wants to merge 1 commit into
base: master
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
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ npm install --save redux-localstorage

## Usage
```js
import {compose, createStore} from 'redux';
import {compose, createStore} from 'redux'
import persistState from 'redux-localstorage'

const enhancer = compose(
Expand Down Expand Up @@ -62,7 +62,7 @@ If, for example, you want to dynamically persist parts of your store state based
paths.forEach((path) => {
if (state[path].persistToLocalStorage)
subset[path] = state[path]
}
})
```

## Immutable Data
Expand All @@ -88,3 +88,19 @@ type config.merge = (initialState: Collection, persistedState: Collection) => fi
```
During initialization any persisted state is merged with the initialState passed in as an argument to `createStore`.
The default strategy `extends` the initialState with the persistedState. Override this function if that doesn't work for you. **Note:** this is only required if you want to merge values within an immutable collection. If your values are immutable, but the object that holds them is not, the default strategy should work just fine.

### Immutable.js
```js
const localStorageConfig = {
slicer: paths => state => (paths ? state.filter((v, k) => paths.indexOf(k) > -1) : state),
serialize: subset => JSON.stringify(subset.toJS()),
deserialize: serializedData => Immutable.fromJS(JSON.parse(serializedData)),
merge: (initialState, persistedState) => initialState.mergeDeep(persistedState),
}

const enhancer = compose(
/* [middlewares] */,
persistState(undefined, localStorageConfig),
)
```