Sub topic 2
Creating your first store with signalStore
This chapter assumes you know Angular components and dependency injection, and the basics of signal and computed. You also need TypeScript object types. Later chapters cover each building block in depth. Here you build the smallest working store and learn how its parts fit together.
What signalStore returns
signalStore is a function from @ngrx/signals. It takes an optional configuration object followed by any number of store features. It returns an injectable class. You do not write the class yourself. Angular's dependency injection creates it, so you use it like any service.
Each feature adds members to the store. The features you meet first are:
withStateadds state as signals.withComputedadds derived signals.withMethodsadds functions that act on the store.
A minimal store
This store holds a counter. It has one state property and two methods.
import { patchState, signalStore, withMethods, withState } from '@ngrx/signals';
type CounterState = { count: number };
const initialState: CounterState = { count: 0 };
export const CounterStore = signalStore(
{ providedIn: 'root' },
withState(initialState),
withMethods((store) => ({
increment(): void {
patchState(store, (state) => ({ count: state.count + 1 }));
},
reset(): void {
patchState(store, { count: 0 });
},
})),
);The pieces work like this:
{ providedIn: 'root' }registers the store as an application-wide singleton, the same option@Injectableaccepts. You can omit it and provide the store yourself, which the scoping chapter covers.withState(initialState)turns every top-level property into a signal. The store now hasstore.count, which is aSignal<number>.withMethodsreceives the store and returns an object of functions. Those functions become public methods.patchStateis the only way to change state. It takes the store and either a partial state object or an updater function, and it replaces the changed properties with new values.
The store's state is read-only from outside. Consumers cannot assign store.count = 5. They call methods, so every change goes through code you wrote.
Using the store in a component
Inject the store and read its signals by calling them.
import { Component, inject } from '@angular/core';
import { CounterStore } from './counter.store';
@Component({
selector: 'app-counter',
template: `
<p>Count: {{ store.count() }}</p>
<button (click)="store.increment()">+1</button>
<button (click)="store.reset()">Reset</button>
`,
})
export class CounterComponent {
readonly store = inject(CounterStore);
}Because count is a signal, the template updates whenever patchState changes it. You do not need async pipes or subscriptions.
Feature order matters
Each feature can use what the features before it added. In the todos store from the next chapters, withComputed reads todos and filter because withState came first. If you put withComputed first, those members do not exist yet and TypeScript reports an error. Write features in the order of their dependencies: state, then computed signals, then methods.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `NullInjectorError: No provider for CounterStore` | The store has no `providedIn: 'root'` and no `providers` entry | Add `{ providedIn: 'root' }` or list the store in a component's `providers` |
| Template shows a function instead of a number | You wrote `store.count` | Call the signal: `store.count()` |
| `Property 'count' does not exist` inside `withComputed` | The feature that adds `count` comes later in the list | Move `withState` before it |
| UI does not update after a change | You mutated an object in place | Return a new object or array from the `patchState` updater |
Exercise
Exercise
A teammate writes signalStore(withState({ count: 0 }), withMethods(...)) with no configuration object, then calls inject(CounterStore) in a component. Angular throws a NullInjectorError. Give two ways to fix it.
Show answer
Pass { providedIn: 'root' } as the first argument to signalStore, so the store is registered application-wide. Alternatively, add CounterStore to the providers array of the component (or a route) that needs it, which gives that scope its own instance.