Sub topic 1
Why SignalStore and where it fits
NgRx SignalStore is a state management library built on Angular signals. This chapter explains what problem it solves, how it differs from the NgRx Store and ComponentStore, and how to decide whether to use it. Later chapters cover each building block in detail.
Prerequisites
This chapter assumes you already know:
- Angular components, templates and dependency injection (
inject(),providedIn). - Angular signals:
signal,computedandeffect. - TypeScript generics and object types.
- Basic RxJS (observables,
pipe,switchMap). You need this later, forrxMethod, but not in this chapter.
You don't need any experience with the NgRx Store. Where it helps the comparison, the chapter describes it.
The problem a store solves
A small Angular component can keep its state in a few signals:
import { Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-counter',
template: `<button (click)="increment()">{{ count() }} / {{ double() }}</button>`,
})
export class CounterComponent {
readonly count = signal(0);
readonly double = computed(() => this.count() * 2);
increment(): void {
this.count.update((n) => n + 1);
}
}This works until state is needed in more than one place. Common triggers:
- Several components need the same data, such as the current user or a shopping cart.
- The state has related parts that must change together, such as a list, a filter and a loading flag.
- Loading logic (HTTP calls, cancellation, error handling) clutters the component.
- You want to test the logic without rendering a template.
A store gathers the state, the values derived from it, and the operations that change it into one injectable unit. Components read from the store and call its methods. They do not own the state.
What SignalStore is
SignalStore is part of the NgRx project and ships in the @ngrx/signals package. You describe a store by composing features inside a signalStore call:
withStatedeclares the state.withComputeddeclares derived signals.withMethodsdeclares operations, including the only code that should change state.withHooksruns code when the store is created or destroyed.
The result is an injectable class. Every piece of state becomes a signal on the store, and computed values and methods sit beside them.
Here is a compact example. Each part is explained in its own chapter, so treat this as a map and not a lesson.
import { computed } from '@angular/core';
import { patchState, signalStore, withComputed, withMethods, withState } from '@ngrx/signals';
type CounterState = { count: number };
export const CounterStore = signalStore(
{ providedIn: 'root' },
withState<CounterState>({ count: 0 }),
withComputed(({ count }) => ({
double: computed(() => count() * 2),
})),
withMethods((store) => ({
increment(): void {
patchState(store, (state) => ({ count: state.count + 1 }));
},
})),
);A component uses it like any service:
import { Component, inject } from '@angular/core';
import { CounterStore } from './counter.store';
@Component({
selector: 'app-counter',
template: `<button (click)="store.increment()">{{ store.count() }} / {{ store.double() }}</button>`,
})
export class CounterComponent {
readonly store = inject(CounterStore);
}Notice three properties of the design:
- State is read as signals.
store.count()is a normal signal read. Templates,computedandeffecttrack it with no extra API. - State is changed in one way. You call
patchState, and the store's own methods wrap it. By default, the state signals are exposed read-only, so a component cannot write to them directly. - The store is composed, not subclassed. You add features in order. Later features can see what earlier ones added, which is how
withComputedcan readcountabove.
The mental model
The flow runs one way. A component never writes state. It asks a method to do the work, and the method updates the state. Because everything downstream is a signal, Angular re-renders only what depends on the changed value.
How SignalStore compares with other NgRx options
NgRx has three state tools. They overlap, so it helps to see what each is built for.
| Aspect | NgRx Store | ComponentStore | SignalStore |
|---|---|---|---|
| Main idea | One global state tree with actions, reducers and selectors | A small class holding state for a component or feature | Composable store built from state, computed and methods |
| Reactivity | Observables (selectors can also be exposed as signals) | Observables | Signals |
| Changing state | Dispatch an action, a reducer returns new state | `setState`, `patchState` and `updater` | `patchState` inside methods |
| Side effects | Effects (separate classes) | `effect` methods | `rxMethod` inside the store |
| Ceremony | High: actions, reducers, selectors, effects | Medium | Low |
| Typical scope | App-wide | One component or feature | Component, feature or app-wide |
| Dev tooling | Redux DevTools, action log | Limited | Not built in; community tools such as the DevTools integration in NgRx Toolkit exist |
A few points behind the table:
- NgRx Store follows the Redux pattern. Every change is a named action, which gives you a log of what happened and strong tooling for time-travel debugging. The cost is boilerplate. It pays off in large apps with many developers and complex cross-feature workflows.
- ComponentStore was the lighter option for local state, but it is built on RxJS. If your code base is moving to signals, it forces you to convert between observables and signals.
- SignalStore keeps most of the structure of a store (single place for state, explicit update path) but drops the action and reducer layer. You get less traceability in exchange for much less code.
SignalStore is not a replacement that makes the NgRx Store obsolete. They can coexist in one app, and teams often keep an existing global store while writing new feature state in SignalStore.
When to choose SignalStore
Choose SignalStore when:
- You already use signals and want state that fits naturally into templates and
computed. - You want a structured place for state, derived values and loading logic, without actions and reducers.
- The state belongs to a feature, a route or a component, and may be shared by a few components.
- You want something easy to unit test, since a store is a plain injectable.
Consider the NgRx Store instead when:
- You need a global, serializable action log or Redux DevTools time travel as a core workflow.
- Many unrelated features must react to the same event, and a published action is the natural way to express that.
- Your organization already has strong conventions built around actions and effects.
A plain signal in a service is enough when:
- The state is one or two independent values.
- No derived state, loading flow or reuse is involved.
A service with a few signals is often the right starting point. Move to SignalStore when the state grows related fields, derived values or async loading, which is when structure starts to pay for itself.
Trade-offs to know up front
- No built-in action history. You cannot see a list of "what happened" without extra tooling. If you need an audit trail, SignalStore alone does not give you one.
- Freedom needs discipline. Because any method can call
patchState, nothing stops you from putting unrelated logic in one large store. Keep stores focused. - State must be updated immutably. Signals decide whether a value changed by comparing references. If you mutate an object in place, dependents may not update. The chapter on
patchStatecovers this in depth. - Version requirements.
@ngrx/signalsneeds a compatible Angular version, and its major version follows Angular's. Check the package's peer dependency range before installing.
To install it in an Angular project, run:
ng add @ngrx/signalsThis adds the package and sets it up for you. Using npm install @ngrx/signals also works.
What the rest of this tutorial covers
The remaining chapters build up the pieces in the order you will use them:
- Creating a store and defining state.
- Updating state with
patchState, and deriving values withwithComputed. - Exposing operations with
withMethods, and reading nested state with deep signals. - Asynchronous loading with
rxMethodandtapResponse, and lifecycle code withwithHooks. - Collections with
withEntities. - Scoping stores to the root or to a component.
- Reusable features with
signalStoreFeature, and testing withTestBed.
Exercise
A feature has a product list, a search term and a loading flag. Three components read them, and the list is loaded over HTTP. The team does not need an action log. Which of these would you pick: a plain signal in a service, SignalStore, or the NgRx Store? Give two reasons.
Show answer
SignalStore. First, the state has related fields (list, search term, loading flag) and a derived view of them, so a single place with an explicit update path keeps them consistent. Second, the async loading logic belongs next to the state, and rxMethod handles it without actions, reducers and separate effect classes. A plain service signal would work at first but gets messy as related fields and loading logic grow. The NgRx Store adds action and reducer boilerplate that the team has no stated need for, since they do not require an action log.
Exercise
Why can't a component write directly to a store's state signals, and what does it do instead?
Show answer
The store exposes its state signals as read-only, so state can only change through code that has access to the store's internals. A component calls a store method, and that method updates state with patchState. This keeps every state change in one place and the data flow one-directional.