rx1984
rx1984

Reputation: 1222

Ngrx select operator does not work

I have a problem with ngrx store, a strange problem! The select operator does not call the callback function, and when I subscribe to the store I can see the state of the app (the initial state and the modified state)

export class PatternsViewComponent implements OnInit {
  patternsCategoryList$:Observable<string[]>;
  // selectedPatternCategory$:Observable<string>;

  constructor(private store: Store<ApplicationState>) {
    this.store.select(mapToDataToSources);
  }
  ngOnInit() {
  }

}
export function mapToDataToSources(state:ApplicationState) :String[]{
  debugger;
  console.log(state.uiStoreData.patterns);
  return state.uiStoreData.patterns;
}

enter image description here

Upvotes: 0

Views: 1951

Answers (2)

tlt
tlt

Reputation: 15191

Be careful with setups like that.

For this purpose, nrgx has createSelector method. you should use that one to create selectors for parts of the store that you want to use later through store.select(whateverSelector).

Bear in mind, store.select doesn't select parts of the store directly but actually goes through selectors that createSelector returns (which handles transforming parts of the store into Observbles).

Upvotes: 2

Meir
Meir

Reputation: 14365

Because your observable (the result of the select) is cold, that is nothing subscribed to it.

Try:

this.store.select(mapToDataToSources).subscribe(); // added .subscribe()

If you use the observable in a view, it is enough that you pipe it through async pipe and the subscribe becomes redundant.

Upvotes: 1

Related Questions