Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redux Toolkit · Function

createEntityAdapter

createEntityAdapter generates reducers and selectors for managing normalized collections of entities. It returns an adapter with CRUD operations, initial-state creation, and selector helpers.

How it works

createEntityAdapter builds an adapter for a normalized collection. It supplies an initial state shaped as ids plus entities, CRUD case reducers, and memoized selectors; optional selectId chooses each entity's key, while sortComparer controls ID ordering.

Syntax

createEntityAdapter<T>(options?: { selectId?: (entity: T) => EntityId; sortComparer?: false | ((a: T, b: T) => number) })

Parameters

NameTypeDescription
optionsEntityAdapterOptions<T>Optional configuration object containing selectId, which returns an entity's unique ID, and sortComparer, which controls entity ordering.

Returns

An entity adapter object containing generated CRUD reducers, the configured selectId and sortComparer callbacks, getInitialState, and selector-generation functions.

Key points

  • The function generates ready-made reducers and selectors for common create, read, update, and delete work on normalized collections.
  • Its options object can specify selectId for identity and sortComparer for ordering.
  • When no identity selector is supplied, the adapter uses each entity's id property.
  • The result is a plain adapter object containing generated operations and selector utilities, not an instance of a class.
  • getInitialState creates the normalized empty collection structure used by the slice.
  • Generated reducer methods can be assigned directly to case reducers in createReducer or createSlice.

Example: Normalized entity slice

import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'

type Book = { id: string; title: string }

const booksAdapter = createEntityAdapter<Book>({
  sortComparer: (a, b) => a.title.localeCompare(b.title),
})

const booksSlice = createSlice({
  name: 'books',
  initialState: booksAdapter.getInitialState(),
  reducers: {
    booksReceived: booksAdapter.setAll,
    bookAdded: booksAdapter.addOne,
  },
})

export const { selectAll: selectAllBooks } = booksAdapter.getSelectors(
  (state: { books: ReturnType<typeof booksAdapter.getInitialState> }) => state.books,
)

Creates a normalized, sorted book collection and uses generated adapter reducers in a slice.

When to use it

Normalized CRUD state

A slice stores many records keyed by stable IDs and needs standard add, update, remove, and replace operations.

Memoized entity selectors

A collection should expose reusable selectAll and selectById accessors from a slice or root state.

Sorted collections

List rendering requires entities to remain ordered according to a comparison function.

Interview questions

QWhat does createEntityAdapter provide?

It creates an adapter object containing generated CRUD reducers, an initial-state factory, and selector generators for normalized entities. The standard state shape is an ids array plus an entities lookup object.

Follow-up: How would you sort entities? Pass sortComparer in the options object; it compares entities and keeps the IDs ordered.

QHow is an adapter used with createSlice?

Use getInitialState() as the slice's initial state, then use adapter methods such as addOne or setAll as case reducers. The same methods can also act as mutating helpers inside Redux Toolkit reducers.

Follow-up: How can selectors read a nested slice? Pass a root-state selector to getSelectors, which returns selectors such as selectAll and selectById.

QHow does the adapter determine entity IDs?

By default, the adapter reads entity.id. For a different identifier field, provide selectId, a function that receives an entity and returns its unique ID.

Follow-up: What happens if nested data is passed to an update operation? The operation performs a shallow merge, so nested incoming objects replace existing nested objects.

  • createSlice
  • createReducer
  • getInitialState
  • getSelectors
  • addOne
  • addMany
  • setAll
  • updateOne

Source: createEntityAdapter. Checked 2026-10-15.