2

Let's say I have the following code and let's say state is from Redux and for some reason state is not yet an Immutable.js object. The problem just cascades and becomes so verbose.

export const helloWorldSelector = createSelector(
  initialState,
  state => state.get('greetings') // yields .get is an undefined function 
);

export const japaneseGreetSelector = createSelector(
  helloWorldSelector,
  state => state.get('ja')
);

export const englishGreetSelector = createSelector(
  helloWorldSelector,
  state => state.get('en')
);

I could make a fallback like so, but this still create problems for the subsequent selectors that depend on this one.

export const helloWorldSelector = createSelector(
  initialState,
  state => state && state.get('greetings')
);

Now if I had longer chain, then I would have to do something like this:

export const helloWorldSelector = createSelector(
  initialState,
  state => state && state.get('greetings', new Map())
);

This is so wet and tedious that I am starting to doubt this pattern. I must be doing something wrong, what is the correct way/best practice to use Immutable.js with Reselect?

Strawberry
  • 66,024
  • 56
  • 149
  • 197
  • I dont understand why would state be not immutable one moment and immutable in an other. is that the desired behaviour – jstuartmilne Oct 17 '19 at 12:06
  • @jstuartmilne Initialization isn't in immutable and I have no control in editing that part of the codebase. I can re-run an initialization myself from my side, but where would I run that code before mapStateToProps runs? – Strawberry Oct 19 '19 at 08:41
  • The sample code confuses me. `createSelector` is from reselect, there are no "plain" selectors and the input to `createSelector` should be a function, not an object? Does the root state really change from plain object to immutable? why would or should it? – dube Nov 20 '19 at 09:19

0 Answers0