useSelector takes a selector function, which receives the entire Redux state and returns whatever specific piece of it the component actually needs — react-redux subscribes the component to the store and, after every dispatched action, re-runs the selector and compares its new result against the previous one using reference equality by default, only triggering a re-render of the component if that specific selected value actually changed. This selective, per-value comparison is what makes useSelector generally more efficient than Context's useContext for frequently-updating state, since a component only re-renders when the exact slice of state it actually reads changes, not whenever any part of the store changes.
1Understanding useSelector
useSelector takes a selector function, which receives the entire Redux state and returns whatever specific piece of it the component actually needs — react-redux subscribes the component to the store and, after every dispatched action, re-runs the selector and compares its new result against the previous one using reference equality by default, only triggering a re-render of the component if that specific selected value actually changed. This selective, per-value comparison is what makes useSelector generally more efficient than Context's useContext for frequently-updating state, since a component only re-renders when the exact slice of state it actually reads changes, not whenever any part of the store changes.
Keep a useSelector selector function narrowly focused on exactly the specific piece of state a component needs, rather than returning a larger object containing extra, unrelated fields — returning a wider object means the component re-renders whenever any of those extra fields change too, even ones it doesn't actually use.
import { useSelector } from 'react-redux';
function CounterDisplay() {
const count = useSelector((state) => state.counter.count);
return <p>Count: {count}</p>;
}2Practical Example
Here is a real-world application of useSelector showing how it is used in production React code.
import { useSelector } from 'react-redux';
function TodoCount() {
const todoCount = useSelector((state) => state.todos.length);
return <p>You have {todoCount} todos.</p>;
}3Best Practices
Follow these guidelines when working with useSelector:
1. Write narrow, focused selector functions that return only the specific value(s) a component actually needs, to minimize unnecessary re-renders from unrelated state changes
2. Use a memoized selector, like one created with Reselect's createSelector, for a selector that performs a genuinely expensive computation over the state
3. Avoid creating a brand-new object or array inside a selector's return value on every call, since useSelector's default reference-equality check would then see a change on every single dispatch regardless of whether the underlying data changed
Tip: Keep a useSelector selector function narrowly focused on exactly the specific piece of state a component needs, rather than returning a larger object containing extra, unrelated fields — returning a wider object means the component re-renders whenever any of those extra fields change too, even ones it doesn't actually use.
import { useSelector } from 'react-redux';
function CounterDisplay() {
const count = useSelector((state) => state.counter.count);
return <p>Count: {count}</p>;
}