reselect icon indicating copy to clipboard operation
reselect copied to clipboard

lodash merge vs object.assign in redux

Open zebapy opened this issue 10 years ago • 1 comments

So I had a reducer like:

export default function entities(state = initialState, action) {
  if(action.response && action.response.entities) {
    return {
      ...state,
      ...action.response.entities
    }
  }

  return state;
}

And some selectors:

export const cardIdSelector = createSelector(
  [
    state => state.cards.cards,
    state => state.entities.cards
  ], (cardIds, cardsById) => cardIds.map(cardId => cardsById[cardId])
);

export const cardSetSelector = createSelector(
  [
    cardIdSelector,
    state => state.entities.sets
  ], (cards, sets) => cards.map(card => ({
    ...card,
    set_id: sets[card.set_id]
  }))
);

I was getting Unhandled promise rejection TypeError: Cannot read property 'set_id' of undefined when I updated state.cards.cards (it's an array of ids. I'm certain it's not mutated in the reducer that updates the value either). It wasn't working until I change my entities reducer to use Lodash's merge instead of spread operators.

Is this a bug or is there a reason I have to use lodash/merge?

zebapy avatar Jan 30 '16 00:01 zebapy

I'm going to close this as it isn't a bug in Reselect, but if you get me a complete example demonstrating the problem that I can run, I'll be happy to take a look for you.

ellbee avatar Jan 31 '16 19:01 ellbee