Read normalized collections
A slice stores entities using createEntityAdapter and components need all records, one record, IDs, or the count.
Redux Toolkit · Module
adapter.getSelectors generates memoized selectors for a normalized entity state. An optional input selector lets those selectors accept the complete Redux state instead of the entity state directly.
getSelectors creates memoized selectors for an entity adapter. With no argument, the selectors expect the entity state itself; with an input selector, they accept the complete Redux state and locate the entity slice before reading IDs, entities, totals, or individual items.
adapter.getSelectors<RootState>(selectState?)| Name | Type | Description |
|---|---|---|
selectState | (rootState) => entityState | Optional selector from the root Redux state to the adapter's entity state. Omit it when selectors should receive the entity state directly. |
A set of memoized selectors: selectIds, selectEntities, selectAll, selectTotal, and selectById. They are either unglobalized selectors over the entity state or root-state-aware selectors when given an input selector.
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'
type Book = { id: string; title: string }
type RootState = { books: ReturnType<typeof booksSlice.reducer> }
const booksAdapter = createEntityAdapter<Book>()
const booksSlice = createSlice({
name: 'books',
initialState: booksAdapter.getInitialState(),
reducers: booksAdapter.getReducers(),
})
export const { selectAll, selectById, selectTotal } =
booksAdapter.getSelectors<RootState>((state) => state.books)
const titles = selectAll({ books: { ids: ['b1'], entities: { b1: { id: 'b1', title: 'Redux' } } } }).map((book) => book.title)
console.log(titles)
Creates root-state-aware selectors for the normalized books slice, then uses selectAll to retrieve entities in stored ID order.
A slice stores entities using createEntityAdapter and components need all records, one record, IDs, or the count.
The adapter's entity state is mounted under a known slice in the root store.
The normalized entity state is nested below another store branch, such as cached data.
It returns memoized selectors for an entity state: selectIds, selectEntities, selectAll, selectTotal, and selectById. The selectors read the normalized IDs and entity lookup table.
Follow-up: How do the selectors become root-state aware? Pass a selector that accepts the entire Redux state and returns the relevant entity state.
Call getSelectors with no argument when the selector input is already the entity state. Pass an input selector such as state => state.books when selectors should receive the root Redux state.
Follow-up: What changes if the entity state is nested in an RTK Query cache? Supply an input selector that reaches the normalized data inside that cache state.
selectAll returns entities in the order represented by the state's ids array, while selectById accepts an entity ID and returns that entity or undefined. selectTotal reports the number of stored entities.
Follow-up: Which normalized structures do these selectors rely on? They read the ids array and entities lookup table maintained by the adapter.
createEntityAdapterselectIdsselectEntitiesselectAllselectTotalselectById