JS Built-ins
redux-legacyselectorbeginner

Selector Functions

πŸ“– Usage Example
function getCounter(state) { return state.counter }
function getUsername(state) { return state.user.name }

// In a component:
const count = getCounter(store.getState())
const name  = getUsername(store.getState())

Selectors decouple components from the shape of the state tree, making refactoring easier.

Selector Functions

Selectors are plain functions that extract a specific piece of data from the Redux state tree:

function getCounter(state) { return state.counter }
function getUser(state) { return state.user }

They centralise access to state shape, so if you rename a key you only update the selector β€” not every component that reads it.

Challenge: Implement three selectors:

  • getCounter(state) β†’ state.counter
  • getUser(state) β†’ state.user
  • getUsername(state) β†’ state.user.name
πŸ’‘ Hints (2)
  • β€’ A selector is just a function: `function getCounter(state) { return state.counter }`.
  • β€’ Keeping selectors in one place means renaming a state key only requires updating the selector.