CHAPTER 18
Beginner
State Management Basics
Updated: May 18, 2026
5 min read
# CHAPTER 18
State Management Basics
1. Chapter Introduction
State is any data your application needs to remember — the current user, a list of products, a shopping cart, or whether a modal is open. As apps grow, "who owns the data and who can change it?" becomes a critical architectural question. Poor state management leads to bugs where two components show conflicting data. This chapter covers scalable state management patterns from simple to advanced.2. Learning Objectives
- Understand types of state (local, shared, global).
-
Implement service-based state with
BehaviorSubject.
- Understand the problems NgRx solves.
- Implement a basic store-like pattern with services.
3. Types of State
text
4. Component-Level State (Local)
Simple boolean flags or form data that only one component needs:
typescript
5. Service-Based State with BehaviorSubject
BehaviorSubject is perfect for shared state. Unlike a regular Subject, it always holds the current value and emits it immediately to new subscribers.
cart.service.ts
cart-icon.component.ts
html
6. Introduction to NgRx
For very large applications with complex state interactions,BehaviorSubject services can become hard to manage. NgRx is Angular's official Redux-style state management library.
NgRx follows a strict unidirectional data flow:
text
typescript
7. Angular Signals (Modern State Management)
Angular 16+ introduced Signals as a simpler reactive primitive:
typescript
8. Choosing the Right State Solution
| Complexity | Solution |
|---|---|
| Single component | Component properties |
| 2-3 related components | Shared Service |
| Feature-level | Service + BehaviorSubject |
| App-wide complex state | NgRx or Angular Signals |
9. Common Mistakes
-
Storing state in multiple places: If both
ComponentAandComponentBkeep their own copy of "current user", they will get out of sync. Have one source of truth.
-
Mutating state directly: Always create new references when updating:
this.items.next([...current, newItem]). Nevercurrent.push(newItem).
10. MCQs with Answers
Question 1
What is "state" in an Angular application?
Question 2
What is the advantage of BehaviorSubject over a regular Subject?
Question 3
What method is used to update a BehaviorSubject's value?
Question 4
What method retrieves a BehaviorSubject's current value without subscribing?
Question 5
What is the unidirectional data flow pattern in NgRx?
Question 6
What Angular 16+ feature provides a simpler alternative to Observables for state management?
Question 7
What is a "single source of truth"?
Question 8
Why should you never directly mutate an array stored in a BehaviorSubject (e.g., using .push())?
Question 9
For a shopping cart feature used across multiple pages, which state solution is most appropriate?
Question 10
What does the async pipe do when used with an Observable in a template?
11. Interview Questions
- Q: Explain the concept of a "single source of truth." Why is it important?
-
Q: Compare service-based state management with
BehaviorSubjectvs NgRx. When would you choose each?
12. Summary
State management is what separates junior from senior Angular developers. Starting with simple service-based state usingBehaviorSubject handles most applications beautifully. For very complex, enterprise-scale state requirements, NgRx provides a predictable, debuggable, and testable solution.