Read a single state value
A component needs one primitive field such as a counter, flag, or username from Redux state.
Redux Toolkit · Hook
useSelector lets a React component read a selected value from the Redux store. It subscribes the component to store updates and returns the selector's result.
useSelector receives a selector that is given the complete Redux store state. It returns the selector's result and subscribes the component to store updates; after an action, the selector runs again, and a changed result causes a re-render. The selector may return an existing value or compute derived data.
useSelector(selector: (state: RootState) => Selected, equalityFn?: EqualityFn): Selected| Name | Type | Description |
|---|---|---|
selector | (state) => selectedValue | A selector function receiving the complete Redux root state and returning the value to expose to the component. |
equalityFn | (previous, next) => boolean | Optional equality comparison function used to determine whether the selected result changed. |
The selected value returned by the selector.
import { useSelector } from 'react-redux'
import type { RootState } from './store'
export function CounterValue() {
const count = useSelector((state: RootState) => state.counter.value)
return <span>{count}</span>
}The component reads the current counter value and updates when that selected value changes.
A component needs one primitive field such as a counter, flag, or username from Redux state.
A view needs a filtered list, computed total, or other value derived from several state fields.
A component displays an entity selected by a route or prop identifier.
useSelector takes a selector function, calls it with the Redux root state, and returns that selector's result. It also subscribes the component to the store and re-runs the selector after dispatched actions.
Follow-up: What happens when the selector returns a new object each time? The changed reference can cause a re-render, so select individual fields or use a memoized selector.
The selector can return a nested state value or derive new data. Its return value becomes the hook's return value, so the component receives exactly the selected result.
Follow-up: How can a selector use an ID? Pass an inline function to useSelector that receives state and calls the real selector with state plus the ID.
The selector is called with the entire Redux store state as its only argument. TypeScript users can annotate that argument with RootState or create a pre-typed hook with useSelector.withTypes<RootState>().
Follow-up: Why prefer a pre-typed hook? It avoids repeating the state type in component selectors and keeps the application's state typing centralized.
useDispatchuseStoreProvidercreateSelectoruseSelector.withTypes