Dispatch typed actions
A component needs to dispatch ordinary Redux actions with application-specific typing.
Redux Toolkit · Hook
useAppDispatch is a pre-typed useDispatch hook for Redux applications written in TypeScript. It exposes the store dispatch function with the middleware-aware AppDispatch type.
useAppDispatch is an application-defined pre-typed version of React-Redux's useDispatch. It is created with the store's AppDispatch type, so components obtain a dispatch function whose type includes middleware-provided capabilities such as thunk dispatching.
export const useAppDispatch = useDispatch.withTypes<AppDispatch>()| Name | Type | Description |
|---|---|---|
| | No runtime parameters; the hook is called with no arguments. |
The Redux store's dispatch function, typed as AppDispatch.
import { useDispatch, useSelector } from 'react-redux'
import type { AppDispatch, RootState } from './store'
export const useAppDispatch = useDispatch.withTypes<AppDispatch>()
export const useAppSelector = useSelector.withTypes<RootState>()
export function AddTodo() {
const dispatch = useAppDispatch()
return (
<button onClick={() => dispatch({ type: 'todos/add', payload: 'Learn Redux' })}>
Add todo
</button>
)
}Creates a typed dispatch hook and uses it in a component to dispatch an action.
A component needs to dispatch ordinary Redux actions with application-specific typing.
A component dispatches thunk functions and needs TypeScript to recognize middleware-provided dispatch overloads.
A codebase wants components to share one standard typed React-Redux hook.
useAppDispatch is a pre-typed wrapper around useDispatch. It returns the store's dispatch function while applying the application's AppDispatch type, including middleware overloads.
Follow-up: Why is AppDispatch preferable to the base Dispatch type? AppDispatch is inferred from the configured store, so middleware-added behavior such as thunk dispatching is represented in the type.
Define AppDispatch as typeof store.dispatch, then create the hook with useDispatch.withTypes<AppDispatch>(). Components import this hook instead of importing plain useDispatch.
Follow-up: How would you keep selector typing consistent? Create a parallel useAppSelector with useSelector.withTypes<RootState>().
A component calls useAppDispatch() to obtain dispatch and then passes an action or thunk to it. This lets the component dispatch store updates without directly importing the store instance.
Follow-up: What enables thunk dispatching here? The configured store's thunk middleware contributes the relevant overloads to typeof store.dispatch.
useDispatchuseSelectoruseAppSelectorAppDispatchRootStateconfigureStore