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

Dispatching Actions with useDispatch

Getting the dispatch function and calling action creators to update Redux state from components.

Dispatching Actions with useDispatch

What you'll learn: How to trigger Redux state changes from your React components using the useDispatch hook.

The Missing Piece

You've learned how to read Redux state with useSelector, but reading is only half the story. To actually change that state—like when a user clicks a button or submits a form—you need to dispatch actions. Think of it like this: useSelector is your read-only newspaper subscription, while useDispatch gives you the pen to write new headlines.

Getting the Dispatch Function

Redux Toolkit provides the useDispatch hook, which returns the Redux store's dispatch function:

import { useDispatch } from 'react-redux';
import { increment, decrement } from './counterSlice';

function Counter() {
  const count = useSelector(state => state.counter.value);
  const dispatch = useDispatch();

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => dispatch(increment())}>+</button>
      <button onClick={() => dispatch(decrement())}>-</button>
    </div>
  );
}

How It Works

When you call dispatch(increment()), here's what happens:

  1. increment() — Your slice's action creator runs, returning an action object like {type: 'counter/increment'}
  2. dispatch(...) — Sends that action to the Redux store
  3. Reducer processes it — Your slice's reducer updates the state
  4. Component re-renders — useSelector sees the new state and your component updates

You're essentially sending a message to the store saying "please run this update."

Common Pattern

Extract dispatch once at the component's top level, then use it in event handlers. Never create it inside loops or callbacks—it's stable across renders.

Key Takeaway: useDispatch gives you a function to send actions to your Redux store, triggering state updates that cause your components to re-render with fresh data.