title: What is the best way to react to ngxs state changes within your component for loading or error events?
body: <p>I have a PostListComponent which iterates over all of the posts retrieved from the server and displays individual PostItemComponent. I am using NGXS for state management. When I initiate the fetchPosts requests, I change the state property loading: true. When request finishes successfully I change it back to false. If a request fails I update the error object on the state and update the error object message property and exception property. </p>
  
  <p>Here is what I have so far </p>
  
  <pre><code>`
  export class PostListComponent implements OnInit {    
    private posts$: Observable&lt;any&gt; = this.store.select(state =&gt; state.posts);
    private isLoading: boolean = false;
    private error: object = {};
  
    constructor(
      public postsFacade: PostsFacadeService,
      public moviesFacade: MoviesFacadeService,
      private store: Store) {    
    }
  
    ngOnInit() {
      this.posts$.subscribe(
        (state) =&gt; this.onLoadingEvent(state)
        );
  
      this.posts$.subscribe(
        (error) =&gt; this.onErrorEvent(error)
      )
      // this.posts$.pipe(
      //   mergeMap(() =&gt; {
      //     // perform the subscription here and listen for state changes
      //   })
      // )
  
    }
  
    onErrorEvent(error)
    {
      this.error = error.error;
    }
  
    onLoadingEvent(state)
    {
      this.isLoading = state.loading;  
    }
  `
  </code></pre>
  
  <p>Structure of my store </p>
  
  <pre><code>`
  @State&lt;PostStateModel&gt;({
      name: 'posts',
      defaults: {
          posts: [],
          loading: false,
          error: {
            message: "",
            error: ""                   
          },
      }
  })
  `
  </code></pre>
  
  <p>I feel like there is a better way of subscribing to state changes within the store with using perhaps the rxjs mergeMap operator. I am not sure if that is possible and if the current approach is efficient. How else can i subscribe to and react to store changes from within my component?</p>