I have and Angular 5 app with ngrx. I have an array of objects in store. When I subscribe to get this array change event is fired even when I don't change state.details.selectedOptions. I changed only state.details.page and subscriber was called.
When I change state.details.selectedOptions to plain value (string) everything is working as expected. As I see the problem is in arrays/nested elements?
//store
const initialState = {
page: null,
selectedOptions: []
}
// selector
export const selectedOptionsSelector = createSelector(
(state: any) => state.details.selectedOptions
)
// also tried such variant
export const selectedOptionsSelector = (state: any) => state.details.selectedOptions
// Component
ngOnInit() {
this.store.select(selectedOptionsSelector).subscribe((res: any) => {
console.log('selectedOptions', res)
})
}
Update:
Basically in my case I always have empty array in selectedOptions because initial state didn't change. I tried using distinctUntilChanged in select but it didn't help
"abc" === "abc", but["abc"] !== ["abc"], so basically every time you use{...}or[...]you are creating a new instance, usingrxjsoperators likedistinctmay be find as a very useful here as it allows to compare new and current value and emit new value only if it differs from the previous one using your custom comparison function - Roomy