Full answer
NgRx is the Redux pattern for Angular. The cycle is: a component dispatches an action, a reducer computes the new state from the old state and the action, the store holds that state, and components select what they need.
Parts:
- Actions describe what happened, not how state changes:
[Courses Page] Load,[Courses API] Load Success.createActionGroupmakes a group with typed props. - Reducers are pure functions
(state, action) => newState. They never mutate and never do side effects.createReducer(initialState, on(action, (state, props) => ({...state, ...}))). - Selectors are functions that read and derive data from state.
createSelectormemoizes: it recomputes only when its inputs change by reference.createFeaturegenerates selectors for each field of a feature state for you. - The store:
inject(Store)andstore.dispatch(action).store.selectSignal(selector)returns a signal.store.select(selector)returns an Observable.
Set-up in a standalone app: provideStore() in ApplicationConfig, and provideState(coursesFeature) for each feature (it can be added to route providers for lazy features). The Redux DevTools extension works with provideStoreDevtools().
Why use it: a single source of truth, predictable state changes, time-travel debugging, and clear logs of "what happened". Costs: boilerplate and indirection. Keep UI-only state out of the store. Do not put server cache and form drafts there without a reason. Keep state normalized, so updates do not need deep copies, and store plain serializable data.
Signals in the store: selectSignal works well with OnPush and computed. For smaller features, NgRx SignalStore gives similar structure with less boilerplate.
import { Component, inject } from '@angular/core';
import { ApplicationConfig } from '@angular/core';
import { createActionGroup, createFeature, createReducer, emptyProps, on, props, provideState, provideStore, Store } from '@ngrx/store';
export interface Course {
id: number;
title: string;
}
export const CourseActions = createActionGroup({
source: 'Courses Page',
events: {
Added: props<{ course: Course }>(),
Removed: props<{ id: number }>(),
Cleared: emptyProps(),
},
});
interface CoursesState {
courses: Course[];
}
const initialState: CoursesState = { courses: [] };
export const coursesFeature = createFeature({
name: 'courses',
reducer: createReducer(
initialState,
on(CourseActions.added, (state, { course }) => ({ ...state, courses: [...state.courses, course] })),
on(CourseActions.removed, (state, { id }) => ({ ...state, courses: state.courses.filter((c) => c.id !== id) })),
on(CourseActions.cleared, () => initialState),
),
});
// createFeature also generates selectCourses, selectCoursesState, and so on.
export const { selectCourses } = coursesFeature;
export const appConfig: ApplicationConfig = {
providers: [provideStore(), provideState(coursesFeature)],
};
@Component({
selector: 'app-course-admin',
template: `
<button (click)="add()">Add course</button>
<button (click)="store.dispatch(actions.cleared())">Clear</button>
<p>{{ courses().length }} courses</p>
<ul>
@for (c of courses(); track c.id) {
<li>{{ c.title }} <button (click)="store.dispatch(actions.removed({ id: c.id }))">Remove</button></li>
}
</ul>
`,
})
export class CourseAdminComponent {
protected readonly store = inject(Store);
protected readonly actions = CourseActions;
protected readonly courses = this.store.selectSignal(selectCourses);
private nextId = 1;
add(): void {
const id = this.nextId++;
this.store.dispatch(CourseActions.added({ course: { id, title: `Course ${id}` } }));
}
}Angular 20.3 with NgRx 20.1, strict template check passes. The reducer is a pure function and can be tested without Angular.
Key points
- Actions are events, reducers are pure state transitions, selectors read and memoize.
- `createFeature` generates the feature selectors. `createActionGroup` types the actions.
- Register with `provideStore` and `provideState`. Read with `selectSignal`.
- Pay the boilerplate cost only where traceability matters.
Common mistakes
- Mutating state in a reducer, for example `state.items.push(x)`.
- Calling an API from a reducer. Side effects belong in effects.
- Putting every piece of UI state in the store.
Follow-ups
- Why must reducers be pure?
- What does memoization in `createSelector` buy you?
- How would you register a feature state for a lazy route?
Tips
- Describe the loop (dispatch, reduce, select) first, then name each piece.
- Be honest about cost, and say when you would pick SignalStore or a service instead.
- Mention immutability and serializable state.
Evaluation rubric
- Explains actions, reducers, selectors, and store flow.
- Shows set-up in standalone APIs.
- Gives a balanced view of costs.