# Redux Toolkit: useDispatch & useSelector

> Connect React components to the Redux store while avoiding catastrophic re-render performance cliffs.

Subject: State Management | Lesson 12 of 22 | 6 min read
Interactive lesson: https://codewithanimation.in/state-management/usedispatch-useselector
Previous: [Redux Toolkit: createSlice & Immer](https://codewithanimation.in/state-management/rtk-createslice-and-immer.md) | Next: [Redux Toolkit: createSelector & Memoization](https://codewithanimation.in/state-management/rtk-createselector-memoization.md)

## Connecting React to Redux

`useDispatch` and `useSelector` are the two primary hooks provided by React-Redux.

`useDispatch` returns the store's dispatch function, allowing your components to fire actions. `useSelector` extracts data from the store state and subscribes the component to future updates.

## The Pre-Typed Hooks Pattern

In modern Redux (v9.1.0+), the official standard is to use `.withTypes<T>()` to create `useAppDispatch` and `useAppSelector` inside a central hooks file.

This prevents you from having to type `(state: RootState)` in every single component, ensures Thunks are typed correctly, and prevents circular dependency issues between your components and your store.

## Strict Equality Subscriptions

When an action is dispatched anywhere in the app, every single `useSelector` hook runs its selector function against the new state.

React-Redux compares the new result with the old result using strict reference equality (`===`). If the results are equal, the component bails out and skips rendering. If they are different, the component is forced to re-render.

## Common mistake: The 100% Re-render Cliff

The biggest mistake developers make is selecting the entire state object: `useSelector(state => state)`. Because Redux is immutable, every single dispatched action results in a brand new root state object reference.

This means `newState === oldState` will evaluate to false 100% of the time, forcing the component to re-render on every single state change anywhere in the application, leading to severe UI jank.

## Tip: Select the Smallest Primitive

Always select the smallest piece of state possible, preferably primitive values like strings or numbers: `useAppSelector(state => state.user.name)`.

Primitives are compared by value. Even if the Redux store creates a brand new state object, the string 'Alice' is strictly equal to 'Alice', allowing React-Redux to safely cancel the component re-render.

## Try it: Interactive Performance Lab

Use the top toolbar to dispatch actions that update different slices of the Redux state. Watch how Component B behaves based on what data its selector is extracting.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/usedispatch-useselector

## Check yourself

### Question 1

Why should you avoid `useSelector(state => state)`?

- A. It violates the Redux toolkit single-file slice pattern.
- B. It forces the component to re-render on every single dispatched action in the entire app.
- C. It mutates the state draft provided by Immer.

Correct answer: B

Because Redux generates a new root state object reference on every update, selecting the entire state guarantees the reference check will fail, forcing a re-render every time.

- Why not A: While it is bad practice, it has nothing to do with the slice pattern.
- Why not C: Selectors read state; they cannot mutate it.

## Remember this

- Create and use `useAppDispatch` and `useAppSelector` to centralize TypeScript typings.
- `useSelector` uses strict reference equality (`===`) to determine if a component should re-render.
- Selecting the entire state object causes catastrophic performance issues by triggering re-renders on every action.
- Always select the smallest piece of state possible, ideally primitive values.
