Back to results for “Collections”
Angular › NgRx State Management

What is the NgRx Entity library, and how does it simplify managing collections of data, like a list of users?

Intermediate

Answer

NgRx Entity provides a standard, normalized way to store collections of similar items, keyed by their unique id, along with prebuilt functions for common operations like adding, updating, and removing entries, saving you from writing repetitive boilerplate code for managing lists of data in your reducers.
export interface UserState extends EntityState<User> {}

export const adapter = createEntityAdapter<User>();
export const initialState = adapter.getInitialState();

export const reducer = createReducer(
  initialState,
  on(addUser, (state, { user }) => adapter.addOne(user, state))
);
Real-world example A user management feature manages its list of users using NgRx Entity, gaining reliable, well tested add, update, and remove operations without needing to hand write that repetitive array manipulation logic themselves.

Common follow-ups: How does storing entities in a normalized, keyed format improve performance compared to a plain array?;What selector functions does NgRx Entity provide automatically out of the box?