Load a complete server snapshot
A fetch operation returns the authoritative current collection; replace stale normalized data with the response.
Redux Toolkit · Module
adapter.setAll replaces an entity adapter's entire normalized collection with supplied entities. It accepts either an array or an ID-keyed record and is commonly used inside Redux Toolkit slice reducers.
Call an entity adapter's setAll reducer with the entity state and either an array of entities or an ID-keyed record. It removes the previous collection and stores exactly the supplied entities, using the adapter's ID and ordering rules.
adapter.setAll(state, entities)| Name | Type | Description |
|---|---|---|
state | EntityState<T, EntityId> | The entity state managed by the adapter, typically the draft state passed to a slice or reducer. |
entities | T[] | Record<EntityId, T> | Replacement entities, supplied as an array or as a record keyed by entity ID. |
The updated entity state; when used as a case reducer, Redux Toolkit handles the resulting state update.
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'
type Todo = { id: string; text: string }
const todosAdapter = createEntityAdapter<Todo>()
const todosSlice = createSlice({
name: 'todos',
initialState: todosAdapter.getInitialState(),
reducers: {
todosReceived(state, action: { payload: Todo[] }) {
todosAdapter.setAll(state, action.payload)
},
},
})
export default todosSlice.reducerThe reducer replaces the adapter-managed collection with the payload array whenever todosReceived is dispatched.
A fetch operation returns the authoritative current collection; replace stale normalized data with the response.
An already-normalized response contains an ID-keyed entities object; store it without converting it to an array.
A slice needs to reset a collection after logout, tenant switching, or another full-context change.
setAll is a generated entity-adapter reducer that replaces the entire normalized collection. It receives the current entity state and either an array of entities or an ID-keyed record.
Follow-up: How would you preserve existing entities while incorporating incoming data? Use addMany or upsertMany instead, depending on whether existing IDs should be left unchanged or updated.
Use it as a case reducer or as a mutating helper inside a createSlice or createReducer case. In a slice, call adapter.setAll(state, action.payload), where the payload contains the replacement collection.
Follow-up: Why is this safe despite the apparent mutation? Redux Toolkit case reducers use Immer drafts, so the helper can express updates through draft mutation while producing immutable state.
The adapter stores normalized data in an entity state with IDs and an entities lookup. setAll rebuilds that collection from the supplied values, applying the adapter's configured ID selection and sorting behavior.
Follow-up: What input is useful when an API response is already normalized? Pass the ID-keyed entities object directly, because setAll accepts that form without requiring an array conversion.
createEntityAdaptergetInitialStateaddManyupsertManyremoveManyselectAll