Redux & Redux Toolkit Course
Redux & Redux Toolkit
/
Advanced

createEntityAdapter()

Definition

An API provided by RTK that automatically generates a set of prebuilt reducers and selectors for performing CRUD operations on normalized state.

Explain Like I'm New

Writing the logic to add, update, and delete items from a normalized `{ ids, entities }` state is tedious. `createEntityAdapter` gives you magic functions like `addOne`, `addMany`, `removeOne`, and `updateOne` out of the box.

Real World Example

You fetch 100 users from an API. You pass the array to `usersAdapter.setAll(state, users)`. It automatically normalizes the array into dictionaries and updates the state.

Common Use Cases

  • •Managing lists of relational data
  • •Standardizing CRUD operations

Interactive Example

Loading Code Playground...

Interview Questions

basic

  • What shape does `createEntityAdapter` enforce on your state slice?

intermediate

  • Does `createEntityAdapter` also generate selectors for you?

Flash Cards

Question

What shape?

Click to reveal answer
Answer

`{ ids: [], entities: {} }`

Question

Generate selectors?

Click to reveal answer
Answer

Yes! It generates `selectAll`, `selectById`, `selectIds`, and `selectTotal`, which are automatically memoized.