Skip to main content

What is EntityAdapter in NgRx?

EntityAdapter in NgRx is a helper that simplifies working with collections of objects (for example, a list of users, products, or posts).

Instead of writing a pile of logic by hand for:

  • adding,
  • updating,
  • removing items by id,

you use ready-made methods.

How it works:

Say you have a list of users.

Instead of storing them as an array, EntityAdapter converts this into a structure:

ts
{ ids: [1, 2, 3], entities: { 1: { id: 1, name: 'Maria' }, 2: { id: 2, name: 'Oleh' }, 3: { id: 3, name: 'Alice' } } }

This makes updates fast and convenient.

What EntityAdapter provides:

  • addOne(entity)
  • addMany([...])
  • updateOne({ id, changes })
  • removeOne(id)
  • setAll([...])

All these methods immediately update ids and entities correctly.

Example:

ts
const adapter = createEntityAdapter<User>(); const initialState = adapter.getInitialState({ loaded: false }); const reducer = createReducer( initialState, on(loadUsersSuccess, (state, { users }) => adapter.setAll(users, { ...state, loaded: true })) );

Conclusion: EntityAdapter is a tool that turns collections into a convenient structure and provides a set of methods for managing them. You write less by hand and make fewer mistakes.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.