Search code examples
ngrxngrx-store

Selector not fired without subscribe()


I have two selectors:

  • isLoggedIn -
  • selectUserDetails

Which implementation looks like this:

export const selectAuthState = state => state.auth;

export const isLoggedIn = createSelector(
    selectAuthState,
    auth => auth.loggedIn
);

export const selectUserDetails = createSelector(
    selectAuthState,
    auth => auth.userDetails
);

Their state looks like this:

export interface State {
  loggedIn: boolean,
  userDetails: UserDetails
}

export const initialAuthState: State = {
  loggedIn: false,
  userDetails: undefined
};

I have a question about this line of code:

ngOnInit() {
    this.isLoggedIn$ = this.store.pipe(
      select(isLoggedIn)
    )

    this.userDetails$ = this.store.pipe(
      select(selectUserDetails)
    )
  }

This part of code is fine:

this.isLoggedIn$ = this.store.pipe(
          select(isLoggedIn)
        )

but this:

this.userDetails$ = this.store.pipe(
      select(selectUserDetails)
    )

is only fired with subscription..

this.store.pipe(
      select(selectUserDetails),
      map(...rest of code)
    ).subscribe();

In other parts of code I also cannot use this selectUserDetails selector without subscribe(). At first I thought that it is a problem with undefined variable but even if user is logged in and userDetails is populated in global State it still is not fired.

Why?


Solution

  • Are you sure, you dont handle isLoggedIn in html file with async pipe?