Getting Started
@angular-redux2/store connects a Redux store to an Angular application. The state is an Angular signal, so a component reads it like any other signal: no async pipe, no subscription, and no decorator.
Install
npm install @angular-redux2/store reduxpnpm add @angular-redux2/store reduxyarn add @angular-redux2/store reduxrxjs and @angular/core are peer dependencies that an Angular application already has. No tsconfig.json setting is needed.
Signals are built into the store: the state is an Angular signal, and every selection is a computed. See Signals for using them with computed, effect, and linkedSignal.
🔁 Coming from 5.x
Version 6 has no decorators. To keep @Select, @Dispatch, and @Substore while you migrate, add @angular-redux2/decorators. Migrating from 5.x explains why they moved and shows both styles side by side.
Describe the state
A slice holds one part of the state, its reducer, and one typed action creator per handler:
import { combineReducers } from 'redux';
import { createSlice } from '@angular-redux2/store';
export const counter = createSlice('counter', 0, {
increment: state => state + 1,
add: (state, amount: number) => state + amount
});
export const rootReducer = combineReducers({ counter: counter.reducer });
export type AppState = ReturnType<typeof rootReducer>;Configure the store
Add provideNgRedux to the application config:
import type { ApplicationConfig } from '@angular/core';
import { isDevMode } from '@angular/core';
import { provideNgRedux } from '@angular-redux2/store';
import { rootReducer } from './store';
export const appConfig: ApplicationConfig = {
providers: [
provideNgRedux({ reducer: rootReducer, devTools: isDevMode(), freeze: isDevMode() })
]
};| Option | Notes |
|---|---|
reducer | The root reducer. |
initialState | The state that the store starts from. Leave it out when the reducers have defaults. |
middleware | Redux middleware, such as a thunk or a logger. |
enhancers | Redux store enhancers, applied after the middleware. |
devTools | true or extension options to connect Redux DevTools. |
freeze | true freezes every state, so a mutation throws. Pass isDevMode(). |
transferState | true carries the state from server-side rendering to the browser. |
The store is created the first time something injects it, and it is ready at that moment.
Read and write state
import { Component, inject } from '@angular/core';
import { injectSelect, NgRedux } from '@angular-redux2/store';
import { counter, type AppState } from './store';
@Component({
selector: 'app-counter',
template: `
<p>{{ count() }}</p>
<button (click)="increment()">+1</button>
`
})
export class Counter {
private readonly store = inject(NgRedux<AppState>);
readonly count = injectSelect((state: AppState) => state.counter);
increment(): void {
this.store.dispatch(counter.actions.increment());
}
}injectSelect returns a read-only signal. Annotate the selector parameter with the state type, and TypeScript infers the selected type. dispatch sends an action, and the signal holds the new value as soon as it returns.
Where to go next
- Example - a complete application with every feature and a unit test.
- NgRedux - the store,
select,select$, and the store options. - Signals - selections with
computed,effect,linkedSignal, and inputs. - Selectors - signals, observables, and when a selection updates.
- Sub-stores - a store scoped to one branch of the state.
- Slices - reducers with typed action creators.
- Draft state - mutating state inside a reducer.
- Mocks - testing components that use the store.
- Migrating from 5.x - from decorators to signals.
- Design decisions - what was removed or left out, and why.
- Benchmarks - version 6 against 5.0.2.
