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

Redux Toolkit · Module

adapter.setMany

adapter.setMany adds or replaces multiple entities in a normalized entity-adapter state. It accepts either an array of entities or an ID-keyed record.

How it works

setMany applies a collection of entities to an entity-adapter state. Each supplied item is identified through the adapter's ID configuration and is inserted when absent or replaces the matching existing entity.

Syntax

adapter.setMany(state, entities)

Key points

  • It processes several entities at once and supports either a list or an object keyed by entity ID.
  • Use the generated method directly as a reducer case or call it inside another Redux Toolkit reducer.
  • Inside Immer-enabled reducers, the helper may be called with draft state using mutation-style syntax.
  • The adapter is an object containing generated CRUD methods rather than a class instance with custom construction semantics.

Common mistakes

  • The input may be either an entity array or an ID-keyed record; passing a different collection shape is unsupported.

Example: Replace multiple entities

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

type Todo = { id: string; text: string; done: boolean }
const todos = createEntityAdapter<Todo>()

const slice = createSlice({
  name: 'todos',
  initialState: todos.getInitialState(),
  reducers: {
    replaceSome(state, action: { payload: Todo[] }) {
      todos.setMany(state, action.payload)
    }
  }
})

const state = slice.reducer(undefined, {
  type: 'todos/replaceSome',
  payload: [{ id: 'a', text: 'Read docs', done: true }]
})
console.log(state.entities.a)

Creates an entity adapter and uses setMany as a case-reducer helper to add or replace the supplied entities.

When to use it

Apply a partial collection response

A fulfilled API response contains several records that should be inserted or replace records with matching IDs.

Batch replacement without clearing the collection

A slice receives a batch of entities and should retain unrelated entities already in state.

Use as a slice case reducer

A reducer needs to update normalized state through a generated entity-adapter helper.

Interview questions

QWhat does adapter.setMany do?

setMany accepts multiple entities and adds or replaces them in the adapter-managed normalized state. It can receive either an array or an ID-keyed record.

Follow-up: How would you preserve existing entities not included in the input? Use setMany rather than setAll, because setMany changes only the supplied IDs.

QHow does setMany decide which entities to replace?

The adapter determines each entity's ID using its configured identity behavior, then updates the normalized collection. Existing matching entries are replaced, while new IDs are inserted.

Follow-up: What if entities use a field other than id? Configure the adapter with selectId so the adapter can derive each entity's identity.

QWhere can setMany be used?

The method is generated by createEntityAdapter and can be used as a case reducer or as a mutating helper inside createSlice or createReducer. The surrounding Redux Toolkit reducer setup handles immutable state updates.

Follow-up: Why might a sorted adapter change the IDs array after setMany? A configured sortComparer can maintain IDs in sorted order as the collection changes.

  • createEntityAdapter
  • addMany
  • setOne
  • setAll
  • upsertMany
  • getInitialState

Source: createEntityAdapter. Checked 2026-10-15.