From d51c216f99ddd0e2e3dc755f31b2815b268ae12a Mon Sep 17 00:00:00 2001 From: Filip Spiridonov Date: Wed, 12 Oct 2016 14:22:26 -0700 Subject: [PATCH] Add a configuration example for Immutable.js For https://github.com/elgerlambert/redux-localstorage/issues/49 --- README.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9aa6127..7096083 100644 --- a/README.md +++ b/README.md @@ -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( @@ -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 @@ -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), +) +``` +