Slice initialization
Define a slice's normalized starting state while keeping loading or error metadata beside entity data.
Redux Toolkit · Module
adapter.getInitialState creates a normalized entity state with ids and entities. It can merge additional slice fields and optionally populate the state with initial entities.
The adapter method creates a fresh normalized state with empty ids and entities collections. You can merge extra state fields into it and optionally provide initial entities, either as an array or an ID-keyed record, so the result is ready for a slice or adapter operations.
getInitialState(): EntityState<T>
getInitialState<S extends object>(state: S): EntityState<T> & S
getInitialState<S extends object>(state: S, entities: T[] | Record<EntityId, T>): EntityState<T> & S| Name | Type | Description |
|---|---|---|
state | S extends object | Optional additional state fields to merge into the normalized entity state. |
entities | Entity[] | Record<EntityId, T> | Optional entities used to pre-populate the returned state; the documentation shows an entity array or Record<EntityId, T>. |
A new EntityState<T>; when additional state is supplied, the result is EntityState<T> & S. With preloaded entities, the returned normalized state contains those entities.
import { createEntityAdapter } from '@reduxjs/toolkit'
type Book = { id: string; title: string }
const booksAdapter = createEntityAdapter<Book>()
const initialState = booksAdapter.getInitialState(
{ status: 'idle' },
[
{ id: 'a', title: 'First' },
{ id: 'b', title: 'Second' },
],
)
console.log(initialState.ids)
console.log(initialState.status)Creates normalized book state, adds a status field, and pre-populates the entity collection.
Define a slice's normalized starting state while keeping loading or error metadata beside entity data.
Provide seed data to an adapter-backed state before reducers process later updates.
Create an empty normalized fallback state for selectors or data transformation workflows.
adapter.getInitialState() return?It returns a new normalized entity-state object whose base shape is {ids: [], entities: {}}. The method is useful as a slice's initial state and can also include extra fields.
Follow-up: How would you initialize loading metadata together with entities? Call getInitialState({status: 'idle', error: null}); the supplied metadata is merged with the normalized fields.
The first argument can be an object containing additional state fields. Those fields are merged into the returned entity state, allowing metadata such as loading status to live beside ids and entities.
Follow-up: Can it start with entities too? Yes. Supply an entity array or an ID-keyed record as the optional preloaded entities argument.
The method supports an overload with an additional state object and an entities argument. The entities may be an array or a Record<EntityId, T>, producing a normalized initial collection.
Follow-up: Why use this instead of manually constructing ids and entities? The adapter applies its entity identity and normalization rules consistently, reducing hand-written setup.
createEntityAdapterEntityStateaddManygetSelectors