Skip to content Skip to sidebar Skip to footer

Ngrx Selector Emits When Anything On The State Changes/memoization Of Selector Not Working

I'm using Angular 7 along with NgRx. I have created a selector to get some data from the store using filter, but this selector emits when anything on the store changes, even if it

Solution 1:

Doing some testing with your example I just notice that ngrx compare the arrays by reference and not by every item inside of it, what this means is that since the function filter return a new array with a new reference ngrx thinks that is different and that is why the subscribe is called every time something changes.

Creating a new copy will have the same effect:

exportconst getMyArrayFilter = createSelector(
    getCounterState,
    state => [ ...state.myArray ]
);

Solution 2:

As Alejandro pointed out, this is because there's a new reference for myArray. You can modify the memoization to your needs, Alex Okrushko does exactly this in his talk NgRx: Selectors are more powerful than you think

enter image description here

enter image description here

Post a Comment for "Ngrx Selector Emits When Anything On The State Changes/memoization Of Selector Not Working"