Merge new API items
Bulk-loading records returned by an API into a normalized slice without replacing existing records.
Redux Toolkit · Module
createEntityAdapter.addMany inserts multiple entities into normalized Redux state, accepting either an array or an ID-keyed record. It skips entities whose IDs already exist.
Call an entity adapter's addMany helper with the current entity state and either an array of entities or an ID-keyed entity object. It inserts only entities whose IDs are not already present; use it as a case reducer or as a mutating helper inside a Redux Toolkit reducer.
adapter.addMany(state, entities)| Name | Type | Description |
|---|---|---|
state | EntityState<T, Id> | The adapter-managed entity state, commonly a draft state inside createSlice or createReducer. |
entities | T[] | Record<EntityId, T> | Entities to insert, supplied as an array or as a record keyed by entity ID. |
An updated entity state when used as a standalone adapter helper; when used as a case reducer, it updates the reducer state through Immer.
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'
type Book = { id: string; title: string }
const booksAdapter = createEntityAdapter<Book>()
const booksSlice = createSlice({
name: 'books',
initialState: booksAdapter.getInitialState(),
reducers: {
booksReceived(state, action: { payload: Book[] }) {
booksAdapter.addMany(state, action.payload)
},
},
})
booksSlice.actions.booksReceived({
type: 'books/received',
payload: [{ id: '1', title: 'Redux' }],
})The adapter helper inserts each previously absent book into the normalized slice state.
Bulk-loading records returned by an API into a normalized slice without replacing existing records.
Calling the helper from a createSlice case reducer to add a payload of entities.
Passing data that has already been converted to a Record<EntityId, T> without another conversion step.
addMany is a generated entity-adapter reducer for bulk insertion into normalized state. It accepts the entity state plus an array of entities or a Record keyed by entity ID, and skips entities already present.
Follow-up: How would you update existing records during the same operation? Use upsertMany, which adds missing entities and updates existing ones.
The helper updates the adapter-managed normalized structure, which stores IDs in an array and entities in a lookup object. It can be supplied as a case reducer or called inside a createSlice reducer.
Follow-up: What determines the key used for each entity? The adapter's selectId configuration determines how an entity ID is obtained when the default id property is not suitable.
Pass the current state as the first argument and the collection as the second: adapter.addMany(state, entities). The collection may already be keyed by IDs, so normalized API output can be passed directly.
Follow-up: What if the incoming data contains nested objects? Normalize or transform that data before inserting it; the adapter manages entity collections rather than recursively normalizing nested values.
createEntityAdapteraddOneupsertManysetAllremoveManygetInitialState