State Management

5 questions found

What is state management in an Angular app?

Beginner
State management refers to how an app organizes and shares data that changes over time, such as the current logged in user, items in a shopping cart, or a form's values. Small apps often manage this using a simple service combined with signals, while larger apps sometimes reach for a dedicated library like NgRx to keep things organized.
@Injectable({ providedIn: 'root' })
export class CartService {
  items = signal<Item[]>([]);

  addItem(item: Item) {
    this.items.update((current) => [...current, item]);
  }
}
Real-world example A small online store manages its shopping cart entirely with a signal based service, letting any component that injects the service read and react to the current cart contents automatically.

Common follow-ups: At what point does an app typically need more than just a simple service for managing its data?;What is the difference between local component state and shared application state?

Angular Signals;Services & Dependency Injection

How do signals simplify state management compared to relying entirely on RxJS observables?

Intermediate
Signals let you read and update a value directly and synchronously, without needing to subscribe or manage a subscription's lifecycle, making many common state management scenarios noticeably simpler to write and reason about, while RxJS remains valuable for more complex asynchronous data streams.
// Signal based, simple and synchronous
count = signal(0);
this.count.set(5);

// Observable based, requires subscribing to actually use the value
count$ = new BehaviorSubject(0);
this.count$.subscribe((value) => console.log(value));
Real-world example A settings service switches its simple boolean flags, like whether dark mode is enabled, from BehaviorSubjects to signals, immediately simplifying the code in every component that reads those values.

Common follow-ups: When would you still prefer an observable over a signal for managing a specific piece of state?;How do signals and observables work together in the same Angular app?

RxJS & Observables;Angular Signals

How do you decide between using a simple signal based service, NgRx, or Angular's built in state features for a specific project?

Advanced
A simple signal based service works well for most small to medium apps, especially with straightforward shared state. NgRx becomes valuable for very large apps with complex, frequently changing shared state across many features, or when a large team needs the strict predictability and tooling that NgRx provides, such as time travel debugging.
// Simple case, a signal based service is enough
@Injectable({ providedIn: 'root' })
export class ThemeService {
  theme = signal('light');
}

// Complex, frequently changing global data, NgRx may fit better
this.store.dispatch(loadDashboardData());
Real-world example A small team app manages its theme preference using a simple signal based service, since it rarely changes, but adopts NgRx for their frequently updating real time order tracking data, since that data's complexity justified the added structure.

Common follow-ups: What specific signs suggest a project has outgrown a simple service based approach?;How easy is it to migrate from a simple approach to NgRx later if the app grows significantly?

NgRx State Management;Angular Style Guide & Best Practices

What does it mean for state to be normalized, and why does this matter for larger Angular apps?

Intermediate
Normalized state stores related data, like users and their posts, as separate flat collections referenced by id, instead of deeply nesting one inside the other. This avoids duplicating the same data in multiple places and makes updating a single piece of information, like a user's name, much simpler and less error prone.
// Not normalized, user data is duplicated inside every post
const posts = [{ id: 1, author: { id: 5, name: 'Alice' } }];

// Normalized, user data lives in one place, referenced by id
const users = { 5: { name: 'Alice' } };
const posts = [{ id: 1, authorId: 5 }];
Real-world example A social media app switches from storing full user objects inside every single post to storing just a user id, avoiding the bug where updating a user's display name previously required updating dozens of duplicated copies across many posts.

Common follow-ups: How do you look up a user's full details from a normalized posts list?;What tools or libraries, like NgRx Entity, help manage normalized state more easily?

NgRx State Management;Design Patterns in Angular

What is a common sign that an Angular app's state management approach needs to be reconsidered?

Beginner
If the same piece of data needs to be passed through many layers of components that do not actually use it themselves, often called prop drilling, or if unrelated parts of an app frequently get out of sync with each other, these are common signs that it might be time to introduce a shared service or a dedicated state management library.
// A sign of trouble, passing a value through several components that never use it directly
<app-layout [user]="user">
  <app-sidebar [user]="user">
    <app-profile [user]="user"></app-profile> <!-- only this component actually needs the user -->
  </app-sidebar>
</app-layout>
Real-world example A team notices they are passing a user object through four unrelated components just to reach one deeply nested component that actually needs it, and decides this is a clear sign to introduce a shared injectable service instead.

Common follow-ups: What other early warning signs suggest state management needs improvement?;Is prop drilling always a bad thing, even in smaller apps?

Services & Dependency Injection;Components & Templates