01The cost of the wrong default
First, the two words this article leans on. State is any data the screen depends on that can change: the list of invoices, whether it's still loading, which row is selected. A store is the class that owns that state and is the only place allowed to change it. The rest of this article is about how big that class should be and where it should live.
Teams often pick a global store because it's what big apps are supposed to use. Then every form toggle needs an action, a reducer and a selector, and simple features take three times as long to build. The opposite failure is state scattered across components with no owner, mutated from anywhere.
There's a middle path that covers most enterprise Angular apps: small, feature-scoped stores built on signals, with private writable state and public read-only access.
02Rules for a signal store
-
01
Private writable, public read-only
Expose
state.asReadonly()andcomputedselectors. Components can read anything but can only change state through named methods. -
02
Scope to the feature, not the app
Provide the store on the feature's shell component. It's created when the feature opens and destroyed when the user leaves, so no stale state survives from last week's visit. Route-level
providerslook similar but outlive the route unless you opt in to cleanup (section 04). -
03
Methods describe intent
approveInvoice(id), notsetInvoices(list). Intent-named methods are easier to review, test and log. -
04
Immutable updates only
state.update(s => ({ ...s, ... })). Combined with OnPush and signals, immutable updates are what make the UI refresh predictably. -
05
Async lives at the edge
Methods call APIs and write results into state. Loading and error are part of the state shape, not separate booleans in each component.
-
06
Plain reads can be resources
On Angular 22, a read that only depends on other state (load this client when the id changes) can be an
rxResourceinside the store. Loading and error come built in, and the store's methods stay for the writes. -
07
Know when to graduate
Many cross-feature consumers, complex undo, or event sourcing and devtools needs are real reasons to adopt NgRx, including its SignalStore.
03When NgRx is the right call
I reach for NgRx when the same state is genuinely shared and edited by many features, when we need time-travel debugging or a full action log, or when the team already knows it well. NgRx SignalStore is a good middle step: it keeps the signal-based API but adds structure and a way to compose stores from reusable features.
The point isn't that stores are bad. It's that the complexity should arrive when the problem does, and a well-encapsulated signal service is easy to migrate later, because components only ever depended on its public API.
Here's what that migration looks like. This is the invoices store from section 04 rewritten with @ngrx/signals 22, the version that supports Angular 22. Components still read items(), loading() and overdue() and still call approve(id), so none of them change.
import { computed, inject } from '@angular/core';
import { patchState, signalStore, withComputed, withMethods, withState } from '@ngrx/signals';
import { InvoicesApi, InvoicesState } from './invoices.api';
export const InvoicesStore = signalStore(
withState<InvoicesState>({ items: [], loading: false, error: null }),
withComputed(({ items }) => ({
overdue: computed(() => items().filter((i) => i.status === 'overdue')),
})),
withMethods((store, api = inject(InvoicesApi)) => ({
load() {
patchState(store, { loading: true, error: null });
api.list().subscribe({
next: (items) => patchState(store, { items, loading: false }),
error: () => patchState(store, { loading: false, error: 'Could not load invoices.' }),
});
},
approve(id: string) {
patchState(store, (s) => ({
items: s.items.map((i) => (i.id === id ? { ...i, status: 'approved' } : i)),
}));
},
})),
);
What you gain is a fixed shape every store follows, state that can only change through patchState inside the store, and optional plugins: withEntities for collections, rxMethod for RxJS-driven methods, and an events plugin (eventGroup, withReducer, withEventHandlers) for teams that want Redux-style events on top of signals. The @ngrx/signals package itself doesn't include a Redux DevTools integration, so if time-travel debugging is the reason you're adopting NgRx, check how you'll get it before you migrate.
04A feature store in about 40 lines
State is private and changed only through methods. Loading and errors are part of the state. The store is provided on the feature's shell component, so its lifetime matches the feature.
import { Injectable, computed, inject, signal } from '@angular/core';
import { Invoice, InvoicesApi } from './invoices.api';
interface InvoicesState {
items: Invoice[];
loading: boolean;
error: string | null;
}
@Injectable()
export class InvoicesStore {
private api = inject(InvoicesApi);
private state = signal<InvoicesState>({ items: [], loading: false, error: null });
readonly items = computed(() => this.state().items);
readonly loading = computed(() => this.state().loading);
readonly error = computed(() => this.state().error);
readonly overdue = computed(() => this.items().filter((i) => i.status === 'overdue'));
load() {
this.state.update((s) => ({ ...s, loading: true, error: null }));
this.api.list().subscribe({
next: (items) => this.state.update((s) => ({ ...s, items, loading: false })),
error: () => this.state.update((s) => ({ ...s, loading: false, error: 'Could not load invoices.' })),
});
}
approve(id: string) {
this.state.update((s) => ({
...s,
items: s.items.map((i) => (i.id === id ? { ...i, status: 'approved' } : i)),
}));
}
}
Where you provide it decides how long it lives. Component providers are destroyed with the component. A route's providers get their own injector, and Angular keeps it after the user navigates away unless you opt in to cleanup with withAutoCleanupInjectors() (stable since Angular 22.2, and your route reuse strategy has to allow it). For a store that should reset whenever the feature closes, a shell component is the simpler choice:
import { Component } from '@angular/core';
import { RouterOutlet, Routes } from '@angular/router';
import { InvoicesStore } from './invoices.store';
// the store lives and dies with this component;
// every page rendered in its outlet can inject it
@Component({
selector: 'app-billing-shell',
imports: [RouterOutlet],
providers: [InvoicesStore],
template: `<router-outlet />`,
})
export class BillingShellComponent {}
export const BILLING_ROUTES: Routes = [
{
path: '',
component: BillingShellComponent,
children: [
{ path: '', loadComponent: () => import('./invoice-list.component').then((m) => m.InvoiceListComponent) },
{ path: ':id', loadComponent: () => import('./invoice-detail.component').then((m) => m.InvoiceDetailComponent) },
],
},
];
The takeaway
Start with feature-scoped signal stores: private state, read-only signals, intent-named methods, and a lifetime tied to the feature. Move to NgRx when shared, cross-feature state actually demands it, and the clean public API makes that move cheap.
Sources
- Hierarchical dependency injection (component providers are destroyed with the component)
- Route API (route
providersget their own environment injector) - withAutoCleanupInjectors (opt-in route injector cleanup, stable since v22.2)
- Async reactivity with resources
- NgRx Signals documentation and the @ngrx/signals package (version 22, Angular 22 support, exported APIs)