Direct case reducer
A slice action needs to delete one normalized record by its identifier.
Redux Toolkit · Module
adapter.removeOne removes one entity from an entity adapter's normalized state by ID. It can be used directly as a case reducer or called as an adapter helper.
removeOne is a CRUD reducer generated by createEntityAdapter. It accepts one entity ID, either directly as a reducer argument or through an action payload when used as a case reducer, and removes the matching entry if present.
removeOne<S extends EntityState<T>>(state: S, key: EntityId): S
removeOne<S extends EntityState<T>>(state: S, key: PayloadAction<EntityId>): S| Name | Type | Description |
|---|---|---|
state | EntityState<T> | The normalized entity state managed by the adapter. |
key | EntityId | PayloadAction<EntityId> | The identifier of the entity to remove, or an action whose payload contains that identifier. |
The updated entity state, with the matching entity removed when it exists.
EntityId and returns the resulting state.adapter.removeOne directly as a reducer definition when the action payload is the entity ID.action.payload, not as a separately named action field.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: {
todoDeleted: todosAdapter.removeOne,
},
})
const nextState = todosSlice.reducer(
todosAdapter.addOne(todosAdapter.getInitialState(), { id: '1', text: 'Read docs' }),
todosSlice.actions.todoDeleted('1'),
)
console.log(nextState.entities['1'])The generated reducer is attached directly to a slice and removes the entity whose ID is supplied in the action payload.
A slice action needs to delete one normalized record by its identifier.
A reducer already has an Immer draft and needs to remove a record during custom logic.
Application code needs the adapter's state transformation outside a slice case reducer.
removeOne do?removeOne is generated by an entity adapter and removes one entity from normalized state using its ID. It does nothing when that ID is not present.
Follow-up: How does it receive the ID in a slice? The generated reducer reads the ID from action.payload.
Its direct signature accepts an entity state and an EntityId, returning the resulting entity state. It also has an overload that accepts a PayloadAction<EntityId>, which lets it serve directly as a case reducer.
Follow-up: What is the related operation for several IDs? removeMany accepts an array of IDs and removes each matching entity.
removeOne be integrated into reducers?The adapter's CRUD methods can be passed directly to createReducer or createSlice. They can also be called manually as mutating helpers when the state value is an Immer draft.
Follow-up: Why does the draft condition matter? Immer can turn draft mutations into the next immutable state, while a normal object must be handled through the helper's returned value.
createEntityAdapterEntityStateEntityIdremoveManyremoveAllcreateSlicecreateReducer