Dispatching synchronous actions
A button, form, or other UI event needs to update Redux state by sending a slice action.
Redux Toolkit · Hook
useDispatch returns the Redux store's dispatch function for use in React components. Call the returned function with actions; in TypeScript, prefer a pre-typed wrapper when middleware adds dispatch capabilities.
useDispatch returns the dispatch function belonging to the Redux store available through React-Redux. Components can call that function with actions, while TypeScript applications can create a pre-typed version so dispatch also understands thunk middleware and other store-specific middleware.
const dispatch: Dispatch = useDispatch()The Redux store's dispatch function, typed as the standard Redux Dispatch by default.
import { useDispatch } from 'react-redux'
import { increment } from './counterSlice'
export function IncrementButton() {
const dispatch = useDispatch()
return <button onClick={() => dispatch(increment())}>Increment</button>
}The component obtains the store's dispatch function and calls it with an action creator result when the button is clicked.
A button, form, or other UI event needs to update Redux state by sending a slice action.
A component starts an asynchronous workflow implemented as a thunk.
A codebase wants consistent, concise Redux hooks across TypeScript components.
useDispatch returns a reference to the dispatch function from the Redux store. Calling the returned function with an action sends that action through the store's dispatch pipeline.
Follow-up: How should it be typed in a TypeScript app? Create a pre-typed useDispatch hook from the store's AppDispatch so thunk and middleware types are included.
Call useDispatch inside a React component to obtain dispatch, then dispatch an action creator result such as dispatch(increment()). The hook lets components dispatch actions without using connect.
Follow-up: What replaces mapDispatch when modernizing a connected component? Define callback functions in the component that call dispatch, commonly using action creators.
The default dispatch type is the standard Redux Dispatch type. It does not include thunk or other middleware extensions, so the store-specific AppDispatch type should be applied to a typed hook.
Follow-up: What is the modern concise way to create that typed hook? Use useDispatch.withTypes<AppDispatch>() and reuse the resulting hook throughout the application.
useSelectoruseStoreuseAppDispatchAppDispatchDispatchProvider