Contents
NgRx Signal Store: Basics and Provider Strategies (Part 1)

NgRx Signal Store: Modern State Management in 10 Minutes

“I need state management. Reactive. Type-safe. No boilerplate.” 🔥

NgRx Signal Store is the answer.

Why NgRx Signal Store?

typescript
// BEFORE (classic NgRx)
interface State { count: number; }
const initialState: State = { count: 0 };
const reducer = createReducer(...)
const actions = createAction(...)
const selectors = createSelector(...)
// 100+ lines of boilerplate 😰

// AFTER (Signal Store)
export const CounterStore = signalStore(
  withState({ count: 0 }),
  withMethods(store => ({
    increment: () => store.count.update(c => c + 1)
  }))
);
// 5 lines. Done. 🎉

Features:

  • ✅ Zero boilerplate
  • ✅ Type-safe with full inference
  • ✅ Reactive through signals
  • ✅ Composable features
  • ✅ An official NgRx solution

Let's begin!


Chapter 1: Quick Start

Step 1: Installation (one minute)

bash
# Install NgRx Signals
npm install @ngrx/signals

# That's it! No other dependencies needed

Step 2: A simple counter store (two minutes)

typescript
// stores/counter.store.ts

import { signalStore, withState, withComputed, withMethods } from '@ngrx/signals';
import { computed } from '@angular/core';

export const CounterStore = signalStore(
  // 1. Define state
  withState({ count: 0 }),

  // 2. Add computed values
  withComputed(({ count }) => ({
    doubled: computed(() => count() * 2),
    isEven: computed(() => count() % 2 === 0)
  })),

  // 3. Add methods
  withMethods((store) => ({
    increment() {
      patchState(store, { count: store.count() + 1 });
    },

    decrement() {
      patchState(store, { count: store.count() - 1 });
    },

    reset() {
      patchState(store, { count: 0 });
    }
  }))
);

Use in a component:

typescript
// counter.component.ts

import { Component } from '@angular/core';
import { CounterStore } from './stores/counter.store';

@Component({
  selector: 'app-counter',
  standalone: true,
  providers: [CounterStore],  // Provide store
  template: `
    <div>
      <h3>Count: {{ store.count() }}</h3>
      <p>Doubled: {{ store.doubled() }}</p>
      <p>Is Even: {{ store.isEven() }}</p>

      <button (click)="store.increment()">+</button>
      <button (click)="store.decrement()">-</button>
      <button (click)="store.reset()">Reset</button>
    </div>
  `
})
export class CounterComponent {
  readonly store = inject(CounterStore);
}

Total time: three minutes!


Chapter 2: Provider Strategies

Strategy 1: providedIn: 'root' (singleton)

typescript
// stores/user.store.ts

import { signalStore, withState, withComputed, withMethods } from '@ngrx/signals';
import { computed } from '@angular/core';

interface User {
  id: string;
  name: string;
  email: string;
}

interface UserState {
  user: User | null;
  loading: boolean;
}

export const UserStore = signalStore(
  { providedIn: 'root' },  // ← SINGLETON!

  withState<UserState>({
    user: null,
    loading: false
  }),

  withComputed(({ user }) => ({
    isAuthenticated: computed(() => user() !== null),
    userName: computed(() => user()?.name ?? 'Guest')
  })),

  withMethods((store) => ({
    setUser(user: User) {
      patchState(store, { user });
    },

    logout() {
      patchState(store, { user: null });
    },

    setLoading(loading: boolean) {
      patchState(store, { loading });
    }
  }))
);

Characteristics:

typescript
scope: "Entire application"
lifecycle: "App bootstrap → app destroy"
sharing: "All components share the SAME store"

// Usage:
class ComponentA {
  store = inject(UserStore);  // Gets THE SAME instance
}

class ComponentB {
  store = inject(UserStore);  // Gets THE SAME instance as A
}

When to use it:

typescript
✅ User authentication state
✅ Global app settings
✅ Theme/language preferences
✅ Shopping cart (single cart per user)
✅ Notifications
✅ Any state shared ACROSS the entire app

❌ Form state (each form has its own)
❌ Component-specific state
❌ Temporary UI state

Strategy 2: Provided in a component

typescript
// stores/todo-form.store.ts

import { signalStore, withState, withMethods } from '@ngrx/signals';

interface TodoForm {
  title: string;
  description: string;
  priority: 'low' | 'medium' | 'high';
}

export const TodoFormStore = signalStore(
  // NO providedIn! Will be provided in component

  withState<TodoForm>({
    title: '',
    description: '',
    priority: 'medium'
  }),

  withMethods((store) => ({
    updateTitle(title: string) {
      patchState(store, { title });
    },

    updateDescription(description: string) {
      patchState(store, { description });
    },

    updatePriority(priority: TodoForm['priority']) {
      patchState(store, { priority });
    },

    reset() {
      patchState(store, {
        title: '',
        description: '',
        priority: 'medium'
      });
    }
  }))
);

// todo-form.component.ts
@Component({
  selector: 'app-todo-form',
  standalone: true,
  providers: [TodoFormStore],  // ← Provided HERE!
  template: `
    <form>
      <input
        [value]="store.title()"
        (input)="store.updateTitle($any($event.target).value)"
      />
      <textarea
        [value]="store.description()"
        (input)="store.updateDescription($any($event.target).value)"
      ></textarea>

      <select
        [value]="store.priority()"
        (change)="store.updatePriority($any($event.target).value)"
      >
        <option value="low">Low</option>
        <option value="medium">Medium</option>
        <option value="high">High</option>
      </select>

      <button type="button" (click)="store.reset()">Reset</button>
    </form>
  `
})
export class TodoFormComponent {
  readonly store = inject(TodoFormStore);
}

Characteristics:

typescript
scope: "Only this component and its children"
lifecycle: "Component create → component destroy"
sharing: "Each component instance has its OWN store"

// Example:
<app-todo-form />  // Own store instance #1
<app-todo-form />  // Own store instance #2
<app-todo-form />  // Own store instance #3

// Each has INDEPENDENT state!

When to use it:

typescript
✅ Form state
✅ Component-specific UI state
✅ Wizard/stepper state
✅ Modal/dialog state
✅ Temporary data editing

❌ Shared state between components
❌ State that survives navigation
❌ Global app state

Strategy 3: Provided in a route

typescript
// Route configuration
const routes: Routes = [
  {
    path: 'products',
    component: ProductsPageComponent,
    providers: [ProductsStore]  // ← Provided for route!
  }
];

// stores/products.store.ts
import { signalStore, withState, withComputed, withMethods } from '@ngrx/signals';
import { computed } from '@angular/core';

interface Product {
  id: string;
  name: string;
  price: number;
}

interface ProductsState {
  products: Product[];
  selectedId: string | null;
}

export const ProductsStore = signalStore(
  withState<ProductsState>({
    products: [],
    selectedId: null
  }),

  withComputed(({ products, selectedId }) => ({
    selectedProduct: computed(() => {
      const id = selectedId();
      return products().find(p => p.id === id) ?? null;
    })
  })),

  withMethods((store) => ({
    loadProducts(products: Product[]) {
      patchState(store, { products });
    },

    selectProduct(id: string) {
      patchState(store, { selectedId: id });
    },

    clearSelection() {
      patchState(store, { selectedId: null });
    }
  }))
);

// products-page.component.ts
@Component({
  selector: 'app-products-page',
  standalone: true,
  template: `
    <app-product-list [products]="store.products$()" />
    <app-product-detail [product]="store.selectedProduct$()" />
  `
})
export class ProductsPageComponent {
  readonly store = inject(ProductsStore);
}

// product-list.component.ts (child)
@Component({
  selector: 'app-product-list',
  standalone: true,
  template: `
    <div *ngFor="let product of products">
      <button (click)="store.selectProduct(product.id)">
        {{ product.name }}
      </button>
    </div>
  `
})
export class ProductListComponent {
  @Input() products: Product[] = [];
  readonly store = inject(ProductsStore);  // Same instance from route!
}

Characteristics:

typescript
scope: "Route component and all child components"
lifecycle: "Route activate → route deactivate"
sharing: "All components in the route tree share the store"

// Navigation flow:
/products → ProductsStore created
  ├─ ProductsPageComponent (uses store)
  ├─ ProductListComponent (uses SAME store)
  └─ ProductDetailComponent (uses SAME store)

/about → ProductsStore DESTROYED

/products → NEW ProductsStore created

When to use it:

typescript
✅ Feature-specific state
✅ Master-detail patterns
✅ Shared state within route
✅ Data loaded for entire section

❌ State needed across routes
❌ Global app state
❌ Independent component state

In the next part: real-world patterns for HTTP, LocalStorage, entities and store composition.

All articles