Getting Started
@angular-redux2/store connects a Redux store to an Angular application. NgRedux holds the store and hands it out as RxJS observables, and a handful of decorators bind state to components without wiring subscriptions by hand.
Install
npm install @angular-redux2/store@5 reduxpnpm add @angular-redux2/store@5 reduxyarn add @angular-redux2/store@5 reduxThe v5.0.x line declares Angular 12 and later, RxJS 7.5 and later, and Redux 4.2.1 and later as peers, and was built and tested with Angular 15.
Describe the state
/**
* Imports
*/
import { combineReducers } from 'redux';
/**
* State
*/
export interface AppStateInterface {
counter: number;
}
export const INITIAL_STATE: AppStateInterface = {
counter: 0
};
export const rootReducer = combineReducers({
counter: (state: number = 0, action: { type: string }) => action.type === 'INCREMENT' ? state + 1 : state
});Configure the store
Configure the store once, before the first component reads from it. A store module that the root module imports is the place:
/**
* Imports
*/
import { isDevMode, NgModule } from '@angular/core';
import { DevToolsExtension, NgRedux, NgReduxModule } from '@angular-redux2/store';
import { INITIAL_STATE, rootReducer } from './store';
/**
* Type-only imports erased during TypeScript compilation.
*/
import type { AppStateInterface } from './store';
/**
* Store module
*/
@NgModule({
imports: [ NgReduxModule ]
})
export class StoreModule {
constructor(ngRedux: NgRedux<AppStateInterface>, devTools: DevToolsExtension) {
const enhancers = isDevMode() && devTools.isEnabled() ? [ devTools.enhancer() ] : [];
ngRedux.configureStore(rootReducer, INITIAL_STATE, [], enhancers);
}
}NgReduxModule provides DevToolsExtension. NgRedux itself is provided in root.
Read and write state
/**
* Imports
*/
import { Component } from '@angular/core';
import { Dispatch, Select } from '@angular-redux2/store';
/**
* Type-only imports erased during TypeScript compilation.
*/
import type { Observable } from 'rxjs';
/**
* Component
*/
@Component({
selector: 'app-counter',
template: `
<p>{{ counter$ | async }}</p>
<button (click)="increment()">+1</button>
`
})
export class CounterComponent {
@Select() readonly counter$: Observable<number>;
@Dispatch
increment() {
return { type: 'INCREMENT' };
}
}@Select() with no argument selects the property named after the field, without its trailing $. @Dispatch sends what the method returns. Both reach the store through NgRedux.store, so the component injects nothing; it could as well inject NgRedux and call select('counter') and dispatch(...) itself.
Where to go next
- NgRedux - configuring the store,
select,dispatch, and the static accessor. - Selectors - property, path, and function selectors, and comparators.
- Decorators -
@Select,@Select$,@Dispatch, and@Substore. - Class reducers - reducers written as classes with typed action creators.
- Draft state - mutating state inside a reducer.
- Mocks - testing components that use the store.
