NgRx Store: Reactive State Management for Angular
NgRx Store is a centralized database for your Angular app's frontend state, providing a single source of truth. Use it in large apps to manage complex, shared state like user sessions. The main footgun is overusing it for simple, local state.
Why it exists
In large Angular applications, managing state that's shared across many components can become chaotic. Passing data through inputs and outputs gets complex, and shared services can become unpredictable stateful bags. NgRx Store provides a structured, predictable pattern for managing this shared, or "global", state.
The mental model
Think of the NgRx Store as your application's client-side database. It's a single, immutable object representing your entire app's state. You don't modify this database directly. Instead, you dispatch "actions"—events describing what happened. "Reducers"—pure functions—then calculate the new state based on the current state and the action. Components subscribe to slices of this state and reactively update when it changes.
How it works
The flow is unidirectional. First, a component dispatches an action (e.g., 'Load Products'). Second, an optional side-effect handler called an Effect can listen for this action to perform tasks like API calls, dispatching a new success or failure action upon completion. Third, a reducer function receives the action and returns a completely new state object, never modifying the old one. Finally, components using "selectors" to query the store receive the new state via an Observable and update their view.
When to use it
Use NgRx Store for complex, shared state that needs to be accessed by many unrelated components in a large application. It's ideal for things like user authentication status, shopping cart contents, or data that needs to persist across different routes. It helps you architect complex apps for long-term success.
When not to use it
Avoid NgRx for state that is purely local to a single component, like the open/closed state of a dropdown or form data that doesn't need to be shared. The boilerplate of creating actions, reducers, and effects is significant overkill for these simple use cases. A standard Angular service or simple component state is often a better choice.
One canonical example
Managing user authentication. A feature state might hold isLoggedIn: boolean and user: object. A login component dispatches a [Login Page] Login action. An AuthEffects class calls the auth API and dispatches [Auth API] Login Success with user data. The authReducer updates the state. A navbar and a profile page can both select isLoggedIn to reactively show or hide content without ever communicating directly.
Interview question
For which scenario is NgRx Store most appropriate in an Angular application?
- a.Storing user authentication status and profile data accessible across multiple routes.Correct
- b.Directly handling all HTTP requests to a backend API without additional services.
- c.Reducing boilerplate for simple data binding in small, isolated components.
- d.Managing the visibility state of a component's local modal dialog.
Why? this is the answer
NgRx Store is designed for managing complex, shared application-wide state, such as user authentication, which needs to be accessed by many unrelated components. While Effects can handle API calls, NgRx's core purpose is state management, not replacing the API interaction layer itself.
Just read this? Test yourself on what you have been reading.
Read the original → ngrx.io
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.
See open roles