Component state and services as state

Intermediate · 14 min

When local component state is enough, and when lifting it into a shared service — with signals as the container — is the right next step.

Before thisServices as shared stateSignals, computed values, and effects

Deborah Kurata · 14 min

Component state and services as state

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Deborah KurataReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • State local to one component (a form's draft value, whether a dropdown is open) belongs in that component — lifting it anywhere else adds indirection with no benefit.
  • The moment two components that aren't parent-child need to see or change the same state, it has to move somewhere both can reach — a shared, injected service, exactly as covered a few lessons back.
  • A service holding signals is real state management already — the question this section answers next is when that plain-service approach stops scaling, and something like NgRx earns its added structure instead.

Most state should stay exactly where it starts

It’s tempting, once you’ve seen a shared service hold application-wide state, to reach for that pattern by default. Resist it. Most state in a real app is genuinely local — whether a specific dropdown is open, the current value of a form field mid-edit, which tab is selected in a widget nobody else on the page cares about. None of that needs to be visible anywhere beyond the one component it belongs to:

export class DropdownComponent {
  isOpen = signal(false);
  toggle() { this.isOpen.update((v) => !v); }
}

There’s no decision to make here beyond “this is a signal, on this component.” Lifting it into a shared service would add a layer of indirection — injection, a separate class, a wider surface area — in exchange for nothing, since no other part of the app has any reason to read or change it.

The actual trigger for lifting state out

The services-as-shared-state lesson a few lessons back covered the mechanism; this lesson is about the judgment call for when to reach for it. The honest trigger is specific, not a general preference: state needs to move out of a single component only once either (a) two components with no parent-child relationship genuinely need to see or mutate the same state, or (b) the state needs to outlive any one component’s lifecycle — surviving a user navigating away from a page and back, say. Short of one of those two conditions actually being true, local state remains correct, and moving it anyway just scatters logic across more files than the problem required.

@Injectable({ providedIn: 'root' })
export class CartService {
  private items = signal<CartItem[]>([]);
  readonly itemCount = computed(() => this.items().length);

  addItem(item: CartItem) {
    this.items.update((current) => [...current, item]);
  }
}

A shopping cart is the textbook case where lifting genuinely earns its keep: a product card deep in a catalog page and a cart icon in the header have no parent-child relationship, and the cart’s contents need to persist as a user browses across multiple unrelated pages. Both conditions are satisfied, and a shared service holding the state — exactly the signal-based pattern from the earlier lesson — is the right answer.

This is already state management — the question is when it’s enough

It’s worth naming plainly: a service holding signals, as above, is state management. There’s no sharper line where “just a service” stops and “real state management” begins — NgRx and similar tools don’t do a categorically different job, they add structure (strict unidirectional data flow, time- travel debugging, a consistent pattern for handling side effects) on top of the same underlying idea. The next two lessons are about exactly when that added structure starts paying for itself, and when a plain service like the CartService above remains the simpler, sufficient answer.

This lesson’s video walks through exactly this progression — local state, lifted state, and the signals powering both — with Deborah Kurata’s typically direct framing of when each choice is the right one. The linked “deeper” video covers the same signals-as-state-container ground from a different angle if a second pass helps.

Check yourself

  1. 01A single component has a boolean controlling whether its own dropdown menu is expanded. Where should that boolean live, and why would lifting it into a shared service be a mistake?

    Directly on that component, as a plain signal or property — nothing else in the app needs to know or care whether this one dropdown is open. Lifting it into a shared service adds a layer of indirection with no actual communication problem to solve, since no other component needs to read or change it.

  2. 02What's the actual trigger for moving a piece of state out of a single component and into a shared service, rather than a general rule like "more state should always live in a service"?

    Specifically: when state needs to be visible to, or mutated by, more than one component that doesn't have a direct parent-child relationship — or needs to outlive any single component's own lifecycle, surviving navigation away and back. Absent either of those triggers, local component state remains the simpler, equally correct choice.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark