State management refers to handling application data consistently across components. In Angular, the state can be:
@Input()
and @Output()
.RxJS provides BehaviorSubject, which helps manage state reactively.
Run:
ng generate service state
Modify state.service.ts
:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class StateService {
private stateSource = new BehaviorSubject<string>('Initial State');
currentState = this.stateSource.asObservable();
updateState(newState: string) {
this.stateSource.next(newState);
}
}
Modify app.component.ts
:
import { Component } from '@angular/core';
import { StateService } from './state.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
currentState!: string;
constructor(private stateService: StateService) {}
ngOnInit() {
this.stateService.currentState.subscribe((state) => {
this.currentState = state;
});
}
changeState() {
this.stateService.updateState('Updated State');
}
}
Modify app.component.html
:
<h2>State: {{ currentState }}</h2>
<button (click)="changeState()">Update State</button>
NgRx is a Redux-inspired library for managing state in Angular applications.
Run:
ng add @ngrx/store
ng add @ngrx/effects
Modify app.state.ts
:
export interface AppState {
message: string;
}
Modify app.actions.ts
:
import { createAction, props } from '@ngrx/store';
export const updateMessage = createAction(
'[App] Update Message',
props<{ message: string }>()
);
Modify app.reducer.ts
:
import { createReducer, on } from '@ngrx/store';
import { updateMessage } from './app.actions';
export const initialState = { message: 'Initial NgRx State' };
const _appReducer = createReducer(
initialState,
on(updateMessage, (state, { message }) => ({ ...state, message }))
);
export function appReducer(state: any, action: any) {
return _appReducer(state, action);
}
app.module.ts
Modify app.module.ts
:
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { updateMessage } from './app.actions';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
message$ = this.store.select((state) => state.app.message);
constructor(private store: Store) {}
updateMessage() {
this.store.dispatch(updateMessage({ message: 'Updated NgRx State' }));
}
}
Modify app.component.ts
:
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { updateMessage } from './app.actions';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
message$ = this.store.select((state) => state.app.message);
constructor(private store: Store) {}
updateMessage() {
this.store.dispatch(updateMessage({ message: 'Updated NgRx State' }));
}
}
Modify app.component.html
:
<h2>State: {{ message$ | async }}</h2>
<button (click)="updateMessage()">Update State</button>
✔ RxJS (BehaviorSubject) is great for local state management.
✔ NgRx provides a structured way to manage global state in large applications.
✔ Use RxJS for simple cases and NgRx when state management grows complex.
✔ Proper state management improves performance, scalability, and maintainability. 🚀