Course contentsShow
← React
Lesson 1736 of 2,39641. State Management EcosystemFree lesson

Connecting Redux to React with useSelector

Reading Redux state in components with useSelector and understanding selector functions and memoization.

Connecting Redux to React with useSelector

What you'll learn: How to read Redux state in your React components using useSelector and why selector functions matter for performance.

The Bridge Between Redux and React

Now that you have a Redux store with slices and actions, you need to read that state in your components. The useSelector hook is your bridge—it subscribes your component to specific pieces of the Redux store and automatically re-renders when that data changes.

How useSelector Works

Import useSelector from react-redux and pass it a selector function—a function that receives the entire Redux state and returns the slice you need:

import { useSelector } from 'react-redux';

function TodoList() {
  // Selector function: (state) => state.slice.property
  const todos = useSelector((state) => state.todos.items);
  
  return (
    <ul>
      {todos.map(todo => <li key={todo.id}>{todo.text}</li>)}
    </ul>
  );
}

The component subscribes only to state.todos.items. If other parts of the store change, this component won't re-render.

Selector Functions and Memoization

Think of selectors as "queries" into your store. Simple selectors like (state) => state.todos.items are fine, but if you're computing derived data, extract it to a function:

const selectCompletedTodos = (state) => 
  state.todos.items.filter(todo => todo.completed);

function CompletedList() {
  const completed = useSelector(selectCompletedTodos);
  // ...
}

Warning: This selector creates a new array every time, causing unnecessary re-renders. Redux Toolkit includes createSelector (from Reselect) for memoization—you'll learn that next.

Multiple Selectors

Call useSelector multiple times for different slices:

const todos = useSelector((state) => state.todos.items);
const filter = useSelector((state) => state.todos.filter);

Each subscription is independent.

Key Takeaway: useSelector connects React components to Redux state with selector functions that determine what data to subscribe to—keep selectors simple or memoize them for performance.