Redux Thunk is a middleware for Redux that enables writing action creators that return functions (thunks) instead of plain action objects. These thunk functions receive the `dispatch` and `getState` methods of the Redux store, allowing for asynchronous logic, conditional dispatching, and interaction with the store's current state. The current stable version is 3.1.0, released in conjunction with major updates to the entire Redux ecosystem, including Redux Toolkit 2.0, Redux core 5.0, and React-Redux 9.0. Its release cadence is closely tied to the broader Redux project releases. A key differentiator is its simplicity and official endorsement as the standard approach for handling async logic in Redux, often being included by default in `configureStore` from Redux Toolkit. It also supports injecting custom arguments, facilitating dependency injection patterns for services or APIs.
npm install redux-thunkVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the manual setup of Redux Thunk with `createStore` and `applyMiddleware` in TypeScript, including a basic asynchronous thunk action.
Update your imports to use named exports: `import { thunk } from 'redux-thunk';` or `const { thunk } = require('redux-thunk');`.Ensure your build tools (Webpack, Rollup, etc.) are configured for modern module resolution. For Node.js, ensure correct `package.json` `type` field usage or use appropriate import/require syntax.
Upgrade your `redux` package to `^5.0.0` or higher. If you cannot upgrade Redux, you may need to stick with `redux-thunk` v2.x.
For v2.x, use `require('redux-thunk').default`. For v3.x, switch to `const { thunk } = require('redux-thunk');`.When using Redux Toolkit, avoid manually adding `thunk` middleware. If you need to customize `thunk` (e.g., with `extraArgument`), use the `getDefaultMiddleware` callback within `configureStore`'s `middleware` option.
Change your import statement to `import { thunk } from 'redux-thunk';`.For v3.x, use `const { thunk } = require('redux-thunk');`. For v2.x, ensure you are using `const thunk = require('redux-thunk').default;`.Cast your `store.dispatch` to `ThunkDispatch` or ensure your `dispatch` parameter in hooks is correctly typed (e.g., `const dispatch = useDispatch<AppDispatch>();` where `AppDispatch` includes `ThunkDispatch`).
Configure the `thunk` middleware with `extraArgument` via `applyMiddleware(thunk.withExtraArgument(myApi))` for manual setup, or within `configureStore`'s `middleware` option: `thunk: { extraArgument: myApi }`.