Handle partial entity edits
A slice reducer receives an action containing an entity ID and changed fields.
Redux Toolkit · Module
adapter.updateOne changes one existing entity in a normalized Redux Toolkit entity state. It identifies the entity with id and shallowly applies the fields in changes.
adapter.updateOne updates an existing entity in a normalized entity state. Provide the target ID and a changes object; the supplied fields are shallowly merged into that entity.
updateOne<S extends EntityState<T>>(state: S, update: Update<T>): S
updateOne<S extends EntityState<T>>(state: S, update: PayloadAction<Update<T>>): S| Name | Type | Description |
|---|---|---|
state | EntityState<T> | Entity state being updated; it must contain the adapter's normalized IDs and entity lookup table. |
update | Update<T> | An object with the target entity ID and a shallow field-update map. |
The updated entity state (S).
changes map.changes; omitted fields remain unchanged.updateOne directly as a slice or standalone reducer case when its action payload matches the expected update shape.import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'
type User = { id: number; name: string; active: boolean }
const usersAdapter = createEntityAdapter<User>()
const usersSlice = createSlice({
name: 'users',
initialState: usersAdapter.getInitialState(),
reducers: {
userRenamed: (state, action: { payload: { id: number; name: string } }) => {
usersAdapter.updateOne(state, {
id: action.payload.id,
changes: { name: action.payload.name },
})
},
},
})
const state = usersAdapter.addOne(usersAdapter.getInitialState(), {
id: 1, name: 'Ada', active: true,
})
usersSlice.reducer(state, usersSlice.actions.userRenamed({ id: 1, name: 'Grace' }))Creates normalized user state and shallowly changes one existing user's name.
A slice reducer receives an action containing an entity ID and changed fields.
A normalized collection needs one existing record changed without rebuilding the collection.
An entity adapter is used with Immer-powered Redux Toolkit reducers.
updateOne accept and do?It takes an update object containing an entity ID and a changes object with one or more new field values. It performs a shallow update on the matching entity.
Follow-up: What happens if changes contains a nested object? The nested value is replaced as a whole because the merge is shallow.
updateOne be used?The method can be used as a case reducer in createSlice or createReducer. It can also be called as a mutating helper inside those Immer-powered reducers.
Follow-up: Why is direct mutation acceptable there? Redux Toolkit uses Immer in those reducer contexts, translating draft mutations into immutable updates.
The update payload has the shape { id, changes }, where id identifies the entity and changes contains fields to overwrite. Only the specified top-level fields are changed.
Follow-up: How would you update a nested field safely? Construct a replacement nested object containing the retained values, or perform a more explicit draft mutation.
createEntityAdapterupdateManyupsertOneupsertManyEntityStateUpdate