reselect
reselect copied to clipboard
lodash merge vs object.assign in redux
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?
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.