Sub topic 1
Why SignalStore and where it fits
Prerequisites
This chapter assumes you know the following:
- Angular components, templates and dependency injection.
- Angular signals:
signal,computedandeffect. - TypeScript generics and object types.
You do not need RxJS for this chapter. It becomes necessary later, in the chapter on rxMethod.
What SignalStore is
NgRx SignalStore is a state management library built directly on Angular signals. You describe a store by composing small pieces: state, computed signals and methods. Angular then provides the store as an injectable service.
A store has three kinds of members:
- State: plain data, exposed as signals.
- Computed signals: values derived from state.
- Methods: the only functions that change state.
Components read signals in their templates and call methods to request changes. They do not change state directly.
How it compares with NgRx Store and ComponentStore
NgRx offers three tools for state, and they solve different problems.
| Tool | Model | Built on | Typical scope |
|---|---|---|---|
| NgRx Store | Actions, reducers, selectors, effects | RxJS observables (signals via `selectSignal`) | Whole application |
| ComponentStore | Class with updaters, selectors and effects | RxJS observables | One component or feature |
| SignalStore | Composed features: state, computed, methods | Signals | Application, feature or component |
NgRx Store gives you a single global state tree. Every change is an action that passes through a reducer, which makes the history traceable and works well with the Redux DevTools. The cost is boilerplate: actions, reducers, selectors and effects for every feature.
ComponentStore is a lighter, class-based store for local state. It is observable-based, so templates need the async pipe or a conversion to signals.
SignalStore keeps the lightweight feel of ComponentStore but uses signals as its native type. It has no actions or reducers. A method calls patchState and the state changes.
When to choose it
Choose SignalStore when:
- Your app already uses signals and you want state that fits that model.
- You want state at the component, feature or application level without writing actions and reducers.
- You want to reuse behavior across stores through composition.
Consider NgRx Store instead when:
- You need strict, explicit event history and want every change recorded as a named action.
- Many unrelated parts of a large app must react to the same events.
- Your team already relies on that workflow and its tooling.
Both can live in one application. You can use the global store for a few shared, event-heavy concerns and SignalStore elsewhere.
A first look
This is the smallest useful store. The next chapters explain each part.
import { patchState, signalStore, withMethods, withState } from '@ngrx/signals';
export const CounterStore = signalStore(
{ providedIn: 'root' },
withState({ count: 0 }),
withMethods((store) => ({
increment(): void {
patchState(store, (state) => ({ count: state.count + 1 }));
},
})),
);A component injects CounterStore, reads store.count() in its template, and calls store.increment(). The count property is already a signal, so no extra wrapping is needed.
Common mistakes
- Treating it as a replacement for every NgRx Store use. SignalStore has no action log. If you need one, it is the wrong tool.
- Changing state from components. Components should call store methods instead of patching state. The store's state is protected from outside writes by default, which keeps changes in one place.
- Putting derived data in state. Values that can be computed from other state belong in
withComputed, not stored twice.
Exercise
A team builds a dashboard with a few local filters in one feature. They want signals in templates, no actions or reducers, and no global state tree. Which NgRx tool fits best, and why?
Show answer
SignalStore. It is signal-based, so templates read values directly. It needs no actions or reducers, and it can be provided at the component or feature level instead of as one global tree. NgRx Store would add boilerplate the team does not need.
Exercise
Name the three kinds of members a SignalStore is composed from, and say which one is allowed to change state.
Show answer
State, computed signals and methods. Methods change state, by calling patchState. Computed signals only derive values, and components should not change state directly.