question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Immutable.js state with redux-localstorage

See original GitHub issue

I’m not sure how to setup the config parameter to properly use an Immutable object, This is how I configure the options:

import Immutable from 'immutable';

export const LocalStorageConfig = {
    serialize: (subset) => subset.toJson(),
    deserialize: (serializedData) => Immutable.fromJS(JSON.parse(serializedData)),
    merge: (initialState, persistedState) => initialState.mergeDeep(persistedState)
};

But I get an error with my selector, state.get is not a function :

export const menuSelector = createSelector(
    mainSelector,
        (state) => ({
            menu: state.get('menu')
    })
);

It looks like I’m missing something here.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
NameFILIPcommented, Jul 15, 2016

I’ve improved slicer from the previous example.

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),
};

Use-case:

persistState(undefined, localStorageConfig)
1reaction
kretzcommented, Nov 21, 2015

I got it to work with the following, which is changed from the initial comment. Also, I was tripped up when combining this with combineReducers and removed it.


const loggerMiddleware = createLogger();

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

const createPersistentStore = compose(
  persistState(['userId', 'idToken'], localStorageConfig),
  applyMiddleware(
    thunkMiddleware,
    loggerMiddleware
  )
)(createStore);

const store = createPersistentStore(rootReducer);

Read more comments on GitHub >

github_iconTop Results From Across the Web

react/redux/immutable/redux persist - Stack Overflow
Have a redux application where the state is composed of Immutable Records. Looking to serialize / deserialize only a subset of my state...
Read more >
Immutable.js vs Redux Persist | What are the differences?
It is a library allowing to save the redux store in the local storage of your browser. It promises to retain the users'...
Read more >
redux-localstorage-simple - npm
Redux -LocalStorage-Simple. Save and load Redux state to and from LocalStorage. Supports Immutable.js data structures.
Read more >
Using Immutable.js with Redux - Medium
The state consists of two leaves, byId and ids; used to store an array of objects (items) with primitive properties. The code is...
Read more >
Mark Erikson on Twitter: "Good post on some reasons to avoid ...
Good post on some reasons to avoid using Immutable.js in Redux apps: ... codify ImmutableJs as a state atom and it has been...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found