Direct case reducer
Define a slice reducer that handles an add action without writing insertion logic.
Redux Toolkit · Module
adapter.addOne is a generated Redux Toolkit reducer for inserting one entity into normalized entity state. It adds the entity only when its ID is not already present and can be used directly as a case reducer or as an Immer helper.
addOne is a generated entity-adapter reducer that inserts one entity into normalized entity state only when its ID is not already present. It accepts either the state plus an entity directly or the state plus a Redux action whose payload is the entity, and it can be used directly as a case reducer or as an Immer-aware helper.
addOne<S extends EntityState<T>>(state: S, entity: T): S
addOne<S extends EntityState<T>>(state: S, action: PayloadAction<T>): S| Name | Type | Description |
|---|---|---|
state | EntityState<T> | The entity state managed by the adapter, containing normalized IDs and entities. |
entity or action | T | PayloadAction<T> | The entity to insert, or a Redux action whose payload contains that entity. |
The updated entity state, with the entity added when its ID is not already present.
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'
type Todo = { id: string; text: string; done: boolean }
const todosAdapter = createEntityAdapter<Todo>()
const todosSlice = createSlice({
name: 'todos',
initialState: todosAdapter.getInitialState(),
reducers: {
todoAdded: todosAdapter.addOne,
},
})
const state = todosSlice.reducer(undefined, {
type: 'todos/todoAdded',
payload: { id: '1', text: 'Read the docs', done: false },
})
console.log(state.entities['1'])Creates an entity adapter and uses addOne as a slice case reducer to insert a Todo.
Define a slice reducer that handles an add action without writing insertion logic.
Add one record while performing additional logic in an Immer-backed reducer.
Maintain a normalized collection keyed by entity IDs.
addOne inserts one entity into an entity adapter's normalized state when that entity's ID is absent. It can be supplied directly as a case reducer to createSlice or createReducer.
Follow-up: How would you replace an existing entity as well as add missing ones? Use the adapter's upsertOne method instead.
The case-reducer form receives an action whose payload is the entity, matching the overload addOne(state, action). The direct helper form receives the entity as its second argument, matching addOne(state, entity).
Follow-up: What must a manually invoked helper receive inside a slice reducer? An Immer Draft state, so the adapter can apply its mutation-style update safely.
An entity adapter manages normalized entity state with an ids collection and an entities lookup. addOne updates that structure by adding the new ID and storing the entity when the ID is not already present.
Follow-up: Which adapter method replaces the whole collection? setAll replaces existing items with the supplied collection.
createEntityAdapterEntityStatecreateSlicecreateReducerupsertOneaddManygetInitialStatesetAll