Normalized CRUD collections
A slice stores many records that are frequently added, updated, or removed by ID.
Redux Toolkit · Type Alias
EntityState<T> is the normalized Redux Toolkit state shape for a collection of entities of type T. It keeps entity IDs in ids and records in the entities lookup.
EntityState<T> is the TypeScript shape used for a normalized collection of entities of type T. It stores entity identifiers in ids and the corresponding records in entities, and is commonly obtained or initialized through createEntityAdapter.
export interface EntityState<T> {
ids: EntityId[]
entities: Record<EntityId, T>
}| Name | Type | Description |
|---|---|---|
T | Type parameter | The entity type stored in the normalized collection. |
A normalized entity-state object with ids and entities fields.
import { createEntityAdapter, type EntityState } from '@reduxjs/toolkit'
type Todo = { id: string; text: string; completed: boolean }
type TodosState = EntityState<Todo>
const todosAdapter = createEntityAdapter<Todo>()
const initialState: TodosState = todosAdapter.getInitialState()
console.log(initialState)
This creates a normalized entity state whose item type is Todo, then initializes it through the adapter.
A slice stores many records that are frequently added, updated, or removed by ID.
Selectors need efficient access to individual records and the complete collection.
A TypeScript slice needs an explicit state type for adapter-managed data.
EntityState<T> represents normalized collection state for entities of type T. Its core fields are ids, an ordered list of identifiers, and entities, a lookup keyed by those identifiers.
Follow-up: Why normalize instead of storing an array? A lookup map avoids duplicating records and lets generated adapter operations update entities by ID.
The type has an ids array and an entities record. The adapter uses that structure to provide CRUD reducers and selectors for the collection.
Follow-up: How is the empty state created? Call the adapter's getInitialState method rather than manually assembling the shape, especially when adding extra slice fields.
EntityState is typically used with createEntityAdapter. The adapter generates reducers for adding, replacing, updating, and removing entities, plus selectors for reading normalized data.
Follow-up: What can setMany accept? It accepts either an array of entities or a Record<EntityId, T> object.
createEntityAdapterEntityIdgetInitialStateaddOneaddManysetAllremoveOneremoveAll