Elkin
Elkin

Reputation: 900

Why ngrx entity selectors trigger actions?

I am learning NgRx but i dont understand why entity selectors trigger actions. I have been searching the reason but i havent found anything. Perhaps i am missing some basics of NgRx/Entity.

This is my demo code:

selectors.ts

export const selectHeroState: MemoizedSelector<object, HeroesState> = createFeatureSelector<HeroesState>('heros');
export const selectHeroes: (state: object) => Hero[] =  heroAdapter.getSelectors(selectHeroState).selectAll;

component.ts

ngOnInit() {
 //this.store$.dispatch(new GetAll()); I thought this line of code was nescessary to fetch all data from store... 
 this.heroes$ = this.store$.select(selectHeroes); //but i just need this one as it triggers the Get All action
}

I hope to have explained my question well. Thanks in advance.

Upvotes: 1

Views: 626

Answers (1)

DeborahK
DeborahK

Reputation: 60528

Selectors don't trigger actions. There must be something else that triggered the action?

For example, in my ngOnInit I have this:

this.store.dispatch(new productActions.Load());

That is what loads my data. Then I can have this:

this.products$ = this.store.pipe(select(fromProduct.getProducts)) as Observable<Product[]>;

Which is the selector to get that data for use in the UI.

Just to confirm, I tried commenting out my dispatch as you have above, but it stopped working (it did not return any data).

So my guess is that somewhere else in the application it is already loading your data?

Upvotes: 5

Related Questions