JS Built-ins
redux-toolkitcreateSlicebeginner

State Selectors

πŸ“– Usage Example
function selectName(state) { return state.user.name }
selectName(store.getState()) // β†’ 'Alice'

Selectors are plain functions that receive state and return a slice of it β€” keeping component logic decoupled from store shape.

State Selectors

Selectors are plain functions that extract data from Redux state. They keep your component code free from knowledge of the state shape.

Challenge: Create a store with a user slice, then write selectName and selectAge selectors.

selectName(store.getState()) // β†’ 'Alice'
selectAge(store.getState())  // β†’ 30
πŸ’‘ Hints (3)
  • β€’ A selector is just a function: `state => state.user.name`.
  • β€’ Selectors decouple components from the exact shape of Redux state.
  • β€’ Call the selector with `store.getState()` to get the current value.