createSelector creates a type-safe selector function for a given store slice. The returned selector extracts that slice’s state from the full store state, or returns undefined if the slice is not configured in the store.
All built-in selectors (selectPlayback, selectVolume, etc.) are created with createSelector. Use it to create selectors for custom slices.
Import
Signature
Parameters
The store slice to create a selector for. The slice’s
name becomes the
displayName of the returned selector.Return value
A selector function
(state: object) => SliceState | undefined. Returns the
slice’s state object when the slice is configured in the store, or undefined
when it is not. The function also has a displayName property set to the
slice’s name.Usage
Create a selector for a custom slice
my-custom-selector.ts
Use with usePlayer in React
CustomFeatureDisplay.tsx
shallowEqual
shallowEqual is a shallow comparison utility exported alongside createSelector. It compares two values one level deep and is the default equality comparator used across useStore, useSnapshot, and useSelector.
shallowEqual explicitly is useful when composing multiple selectors into a derived object and you want to avoid unnecessary re-renders:
ComposedSelector.tsx
shallowEqual signature
true when both values are identical (Object.is) or when they are
non-null objects with the same own keys and identical values for each key.
Notes
- The selector returns
undefinedwhen the slice has no state keys or when the slice is not part of the current store configuration. Always guard againstundefinedin your render logic. - Selectors created with
createSelectorare plain functions and can be used outside React — for example, withPlayerControllerandStoreControllerin HTML environments.