DEV Community

Cover image for Angular State Management with RxJS: Complete Guide | Observables, Subjects & BehaviorSubjects
Md. Maruf Rahman
Md. Maruf Rahman

Posted on • Edited on • Originally published at practicaldev.online

Angular State Management with RxJS: Complete Guide | Observables, Subjects & BehaviorSubjects

When I first started building Angular applications, I thought I needed Redux or NgRx for state management. Then I realized that Angular is built on RxJS, and I could manage state effectively using just Observables, Subjects, and BehaviorSubjects. This approach is lighter, simpler, and works perfectly for most applications.

RxJS is Angular's foundation for reactive programming. It provides powerful tools for managing asynchronous data streams, which makes it perfect for state management. Using BehaviorSubject to store state, exposing Observables for components to subscribe to, and using RxJS operators for derived state, you can build robust state management without external libraries.

What is RxJS State Management?

RxJS State Management provides:

  • Reactive state - State as Observable streams
  • BehaviorSubject - Stores current state value
  • Subjects - Event streams and notifications
  • Operators - Transform and combine state
  • No external libraries - Built into Angular
  • Type-safe - Full TypeScript support
  • Testable - Easy to test with RxJS testing utilities

BehaviorSubject for State

Create a state management service:

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

interface AppState {
  user: any;
  businesses: any[];
  selectedBusiness: any;
}

@Injectable({
  providedIn: 'root'
})
export class StateService {
  private stateSubject = new BehaviorSubject<AppState>({
    user: null,
    businesses: [],
    selectedBusiness: null
  });

  public state$ = this.stateSubject.asObservable();

  getState(): AppState {
    return this.stateSubject.value;
  }

  setState(partialState: Partial<AppState>): void {
    this.stateSubject.next({
      ...this.stateSubject.value,
      ...partialState
    });
  }

  getUser$(): Observable<any> {
    return this.state$.pipe(map(state => state.user));
  }

  getBusinesses$(): Observable<any[]> {
    return this.state$.pipe(map(state => state.businesses));
  }
}
Enter fullscreen mode Exit fullscreen mode

Using State in Components

Subscribe to state changes:

import { Component } from '@angular/core';
import { Observable } from 'rxjs';
import { StateService } from './state.service';

export class BusinessListComponent {
  businesses$: Observable<any[]> = this.stateService.getBusinesses$();

  constructor(private stateService: StateService) {}

  selectBusiness(business: any): void {
    this.stateService.setState({ selectedBusiness: business });
  }
}
Enter fullscreen mode Exit fullscreen mode
<!-- Template with async pipe -->
<div *ngIf="businesses$ | async as businesses">
  <div *ngFor="let business of businesses">
    {{ business.name }}
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Use BehaviorSubject for state - Stores current value
  2. Use Subject for events - Event streams and notifications
  3. Always unsubscribe - Or use async pipe
  4. Use async pipe in templates - Automatic subscription management
  5. Use distinctUntilChanged - Prevent unnecessary updates
  6. Create specific selectors - For derived state
  7. Keep state services focused - Single responsibility

πŸ“– Read the Complete Guide

This is just a brief overview! The complete guide on my blog includes:

  • βœ… RxJS Operators for State - map, filter, distinctUntilChanged, shareReplay, debounceTime, switchMap, combineLatest
  • βœ… Subject for Events - Event streams and notifications
  • βœ… Advanced State Patterns - State with Actions, Effects, and Selectors
  • βœ… Component Patterns - takeUntil pattern, async pipe best practices
  • βœ… Error Handling - Complete error handling patterns
  • βœ… When to Use RxJS vs NgRx - Decision framework
  • βœ… Common Patterns - State service structure, error handling
  • βœ… Real-world examples from production applications

πŸ‘‰ Read the full article with all code examples here


What's your experience with RxJS state management? Share your tips in the comments! πŸš€

For more Angular guides, check out my blog covering Services, Component Communication, Reactive Forms, and more.

Top comments (0)