← Back to Angular, page 5

angular-0092 · Hard senior

How do you choose a state management approach in Angular: component signals, a service with signals, NgRx Store, SignalStore or the router and server cache?

Start with the smallest tool that works: local component state for UI details, a signal service for state shared by a few components, and the URL for state that should survive a refresh or be shareable. Add NgRx Store or SignalStore when many features share complex state, need strict traceability, or need dev tools and effects. Keep server data in a cache layer such as resource, not duplicated in the store.

Full answer

Think of state by where it lives and who needs it, not by tool.

Kinds of state:

  1. UI state (a dropdown is open, a tab is selected): component signals. Never put this in a global store.
  2. Form state: the forms API. Keep unsaved values local until submit.
  3. URL state (page number, filters, selected id): router params and query params. It gives deep links and back-button support for free.
  4. Server state (courses from the API): it has loading, error, staleness and refresh. Use resource/httpResource, a query library, or a dedicated service with caching. Copying it into a global store creates a second source of truth that goes stale.
  5. Shared client state (the shopping cart, an unsaved edit that spans several screens, user preferences): this is where store choices matter.

Options for shared client state:

  • Signal service (@Injectable with private signals, public asReadonly() and methods): the simplest option. Little code, no library, easy to test. It does not enforce structure, so rules live in the team's habits.
  • NgRx SignalStore: a structured signal store with state, computed values, methods and features (entities, hooks, dev tools). Less boilerplate than Store, and strong conventions per feature.
  • NgRx Store with effects: actions, reducers, selectors and effects. More boilerplate, but every change is a named action, giving time-travel debugging, a clear audit trail, and strong conventions in large teams. Best when many developers work on complex, strongly interrelated state and need the trace.
  • Other libraries (Akita, Elf, NGXS) are valid choices, but check maintenance and community support.

Decision signals: how many consumers, how complex the transitions are, whether you need an audit log or undo, team size and familiarity, and whether the state must be shared across lazy-loaded features.

Anti-patterns: one global store for everything, selectors that duplicate derived values, storing derived state instead of computing it, and mutating state. Start simple, and move up when a concrete pain appears, such as unclear update order or debugging difficulty.

typescript
// file: cart.store.ts
import { computed, signal } from '@angular/core';

export interface CartItem {
  id: number;
  name: string;
  price: number;
  qty: number;
}

export class CartStore {
  private readonly _items = signal<readonly CartItem[]>([]);

  readonly items = this._items.asReadonly();
  readonly count = computed(() => this._items().reduce((sum, i) => sum + i.qty, 0));
  readonly total = computed(() => this._items().reduce((sum, i) => sum + i.qty * i.price, 0));

  add(item: Omit<CartItem, 'qty'>): void {
    this._items.update((list) => {
      const existing = list.find((i) => i.id === item.id);
      return existing
        ? list.map((i) => (i.id === item.id ? { ...i, qty: i.qty + 1 } : i))
        : [...list, { ...item, qty: 1 }];
    });
  }

  remove(id: number): void {
    this._items.update((list) => list.filter((i) => i.id !== id));
  }
}

// file: demo.ts
import { CartStore } from './cart.store.ts';

const cart = new CartStore();
cart.add({ id: 1, name: 'Samosa', price: 15 });
cart.add({ id: 1, name: 'Samosa', price: 15 });
cart.add({ id: 2, name: 'Tea', price: 10 });
console.log(cart.count(), cart.total());
cart.remove(1);
console.log(cart.count(), cart.total());

Angular 20.3, type-checked. The sample is a minimal signal service for shared client state and is executed in Node (the signals API works without a browser), twice with identical output.

Key points

  • Classify state first: UI, form, URL, server, shared client.
  • URL and server state have better homes than a global store.
  • Signal service for simple sharing. SignalStore for structure. NgRx Store for strict traceability at scale.
  • Do not store derived data. Start simple and upgrade for a real reason.

Common mistakes

  • Putting everything in NgRx because it is "the standard".
  • Duplicating API data in a store and in a cache, so two copies disagree.
  • Storing values that could be computed from other state.

Follow-ups

  • How would you migrate a service with BehaviorSubjects to signals?
  • What would make you add NgRx Store to an app that uses a signal service?
  • How do you keep state when the user refreshes the page?

Tips

  • Start with the kinds of state. It shows you do not apply one tool everywhere.
  • Say what each option costs, not only what it gives.
  • Mention the URL as state. Many candidates forget it.

Evaluation rubric

  • Separates the kinds of state.
  • Compares options with honest trade-offs.
  • Gives clear criteria for upgrading.