Angular State Management: Where Should Each Value Live?
In this article
- Background: Why State Management Gets Complicated
- Component State: The Default Home
- Shared UI State: Services with Signals
- Server State: Cache and Sync
- URL State: The Router as the Source of Truth
- How to Choose: A Decision Framework
- Common Problems and Misconceptions
- Costs and Trade-offs
- Alternatives and Future Directions
- Frequently Asked Questions
In Angular, the answer to where each value should live depends on its scope and lifespan: component state stays in the component, shared UI state goes in a service (often with signals), server state is cached in a service or a library like NgRx, and URL state belongs in the router. This article breaks down each category with concrete examples and trade-offs so you can make the right call for your app.
Background: Why State Management Gets Complicated
Angular applications are made of components that form a tree. Each component has its own view and can pass data down through inputs and emit events up through outputs. This works well for small apps, but as the app grows, you often need to share data between unrelated components, keep data in sync with the server, and preserve UI state across navigation. That's when developers reach for state management libraries like NgRx, Akita, or NGXS, but adding a library before you need it can add unnecessary complexity. The key is to understand the different types of state and where each naturally belongs.
Component State: The Default Home
Component state is any data that only affects that component's template or its direct children. Examples include a form input value, a toggle flag, or a list of items fetched for a specific view. This state should live in the component class as a property, often wrapped in a signal for fine-grained reactivity.
For example, a search box that filters a local list:
- Store the search term in a signal:
searchTerm = signal(''). - Derive the filtered list with a computed signal.
- No service or library needed.
When you lift state to a service or store too early, you make the code harder to reason about and test. Keep it local until you have a concrete reason to share it.
Shared UI State: Services with Signals
When two or more unrelated components need to read and update the same piece of data, that state should live in a singleton service. Common examples: a sidebar open/closed flag, a theme preference, or a user's selected language.
In modern Angular (v16+), you can use signals inside a service:
@Injectable({ providedIn: 'root' })
export class UiService {
sidebarOpen = signal(false);
toggleSidebar() {
this.sidebarOpen.update(v => !v);
}
}Components inject the service and read the signal. This is simple, testable, and avoids the boilerplate of a full state management library. For more complex shared state with undo/redo or heavy async logic, you might consider a library, but start with a service.
Server State: Cache and Sync
Data that comes from an API—like a list of products, a user profile, or a chat thread—is called server state. It has special concerns: loading, error, stale-while-revalidate, and cache invalidation. The recommended approach is to keep a cache in a service, often using a library like TanStack Query (now supporting Angular) or NgRx's entity and effects.
If you use a service with signals, you can store the current value and a loading flag:
private products = signal<Product[]>([]);
readonly loading = signal(false);
async loadProducts() {
this.loading.set(true);
try {
const data = await http.get('/products').toPromise();
this.products.set(data);
} finally {
this.loading.set(false);
}
}When multiple components need the same data, they all call the service and get the same signal. The service acts as the single source of truth. For advanced features like retries, background refetching, and request deduplication, consider a dedicated library.
URL State: The Router as the Source of Truth
Any state that affects what the user sees in the address bar—like the current page, a selected item ID, a search query, or pagination—should live in the Angular Router. This makes the state shareable via URL, bookmarkable, and preserved on refresh.
For example, a product list with a selected product ID:
- Use route parameters:
/products/:id. - Use query parameters for filters:
/products?category=shoes. - Read these in the component with
ActivatedRouteor the newinput()function for route params.
Putting this data in a service instead of the URL means users lose their place on refresh and can't share a link to a specific view. Always prefer the router for anything that defines the current navigation state.
How to Choose: A Decision Framework
When you're about to add a new piece of state, ask these questions in order:
- Is it only used by one component? → Keep it in that component.
- Is it used by multiple components but not tied to the URL? → Put it in a shared service (with signals or a store).
- Does it come from the server? → Treat it as server state and cache it in a service or library.
- Does it affect the URL or should it be shareable? → Move it to the router.
If you find yourself passing a lot of data through inputs just to avoid a service, that's a sign you need a service. If you have a service with dozens of signals and complex update logic, consider a formal store like NgRx.
Common Problems and Misconceptions
One common mistake is putting all state in a global store “just in case.” This leads to over-engineering and makes it hard to trace where a value changes. Another is storing server data in component state and re-fetching on every navigation, causing unnecessary network requests and flicker.
A frequent question: “Should I use NgRx or a service?” The answer depends on the size of your team and the complexity of your state. NgRx provides a strict pattern with reducers and effects, which helps for large apps with many developers, but it adds a learning curve and boilerplate. For many apps, a service with signals is enough.
Another misconception is that signals replace state management libraries. Signals are a primitive; they don't provide devtools, undo/redo, or effects for side effects. Libraries build on top of signals (NgRx is moving to signals) to offer those features.
Costs and Trade-offs
Each choice has a cost. Component state is free but doesn't share. Services are cheap but can become messy if you don't organize them. The router adds URL complexity but gives you deep linking. Libraries like NgRx give you a robust pattern but require learning concepts like actions, reducers, and effects, and can be overkill for small apps.
Also consider performance: signals provide fine-grained updates, but if you put everything in a single global store, any change might trigger many components to re-check. Keeping state local where possible reduces unnecessary change detection.
Alternatives and Future Directions
Beyond the classic trio (component, service, router), there are libraries that blur the lines. TanStack Query focuses on server state and handles caching, retries, and invalidation automatically. For local UI state, you can use a simple service with signals. For complex client state, NgRx, Akita, and NGXS are all viable, but they are converging on signals as the reactive primitive.
Angular's new signal-based components and the input() and output() functions are making it easier to write reactive code without a library. The future likely involves more built-in reactivity and less need for external stores for simple cases.
Frequently Asked Questions
Should I use NgRx for every Angular app?
No. NgRx is designed for large, complex applications with many developers and intricate state flows. For a small or medium app, a service with signals is simpler and easier to maintain. Start with services and only add NgRx when you hit concrete pain points like unpredictable state updates or too much boilerplate.
What's the difference between a service and a store?
A service is a plain class that can hold state and methods. A store (like NgRx) is a more formal structure with a single state object, actions, and reducers that enforce a unidirectional data flow. Stores provide better tooling and traceability but require more upfront design.
How do I handle server state without a library?
You can create a service that caches the data in a signal and exposes methods to fetch and update it. Handle loading and error states with separate signals. This works well for simple cases, but you'll need to manually manage cache invalidation, retries, and race conditions. Libraries like TanStack Query automate these.
When should I put state in the URL?
If the state defines a specific view that a user might want to bookmark or share, put it in the URL. Examples: selected product ID, search query, pagination page, active tab. If the state is transient (like a dropdown open), keep it in the component.
Can I use signals with NgRx?
Yes. NgRx has introduced signal-based APIs that let you create stores with signals instead of the classic Observable-based approach. This reduces boilerplate and integrates better with Angular's new reactive primitives. Check the official NgRx docs for the latest signal store.