Sobes.tech
Middle

If not NgRx, what would you use instead?

sobes.tech AI

Answer from AI

If you do not use NgRx for state management in an Angular application, you can consider the following alternatives:

  • RxJS with services: use BehaviorSubject or ReplaySubject inside Angular services to store and propagate state.
  • Akita: a lightweight state management library with a simple API and reactive approach.
  • NgXs: a simpler and more declarative state manager than NgRx.
  • Redux: a classic approach that can be integrated with Angular but requires additional setup.
  • Simple local component state: if the application is small, you can manage state within components and pass data via Input/Output.

Example of using BehaviorSubject for state:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class CounterService {
  private countSubject = new BehaviorSubject<number>(0);
  count$ = this.countSubject.asObservable();

  increment() {
    this.countSubject.next(this.countSubject.value + 1);
  }

  decrement() {
    this.countSubject.next(this.countSubject.value - 1);
  }
}
If not NgRx, what would you use instead? — Frontend - sobes.tech