Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsSep 12, 2026·3 min de lecture

Redux Thunk — Async Logic Middleware for Redux

Redux Thunk lets Redux dispatch functions instead of plain action objects, enabling side effects like API calls and conditional dispatching in a straightforward way.

Prêt pour agents

Installation avec revue préalable

Cet actif nécessite une revue. Le prompt copié demande un dry-run, affiche les écritures, puis continue seulement après confirmation.

Needs Confirmation · 64/100Policy : confirmer
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Redux Thunk Middleware
Commande avec revue préalable
npx -y tokrepo@latest install a9a5e703-ae4a-11f1-9bc6-00163e2b0d79 --target codex

Dry-run d'abord, confirmez les écritures, puis lancez cette commande.

Introduction

Redux Thunk is the original middleware for handling asynchronous logic in Redux applications. By allowing dispatch to accept functions alongside plain objects, it provides a minimal but effective pattern for side effects such as API calls, timers, and conditional dispatching based on current state.

What Redux Thunk Does

  • Intercepts dispatched functions and invokes them with dispatch and getState as arguments
  • Enables async/await patterns directly inside action creators
  • Allows conditional dispatching by reading current state before deciding what to dispatch
  • Composes with other Redux middleware in the standard applyMiddleware chain
  • Supports an optional extra argument for injecting services like API clients

Architecture Overview

Redux Thunk is a middleware that sits between dispatching an action and the moment it reaches the reducer. When store.dispatch receives a function instead of an object, the middleware calls that function with dispatch and getState. The thunk function can then perform async work, dispatch multiple actions over time, or inspect state to decide its next step. At roughly 14 lines of source code, it is one of the smallest middleware libraries in the ecosystem.

Self-Hosting & Configuration

  • Install via npm or yarn and add to your Redux middleware stack
  • Pass thunk to applyMiddleware when creating the store
  • Optionally provide an extraArgument via thunk.withExtraArgument(api) for dependency injection
  • Redux Toolkit includes thunk by default in configureStore, so no manual setup is needed with RTK
  • Works with any Redux-compatible framework binding (React, Angular, Vue)

Key Features

  • Minimal footprint at under 20 lines of code with zero dependencies
  • Supports both callback and async/await patterns for async actions
  • Provides access to getState inside thunks for conditional logic
  • Bundled by default in Redux Toolkit via configureStore
  • Compatible with TypeScript through well-maintained type definitions

Comparison with Similar Tools

  • Redux Saga — Uses generator functions for complex async flows; more powerful but steeper learning curve
  • Redux Observable — RxJS-based; ideal for streams and cancellation but adds a large dependency
  • RTK createAsyncThunk — Built on Redux Thunk with added lifecycle actions and error handling
  • Redux Promise — Simpler but limited to single-dispatch promise resolution
  • Zustand / Jotai — Alternative state managers that handle async natively without middleware

FAQ

Q: Is Redux Thunk still relevant with Redux Toolkit? A: Yes. Redux Toolkit uses thunk internally, and createAsyncThunk is built on top of it. Understanding thunks remains essential.

Q: Can I use async/await in thunks? A: Yes. Thunk functions can be async, and dispatch calls inside them work as expected with await.

Q: How do I test thunk action creators? A: Call the thunk with mock dispatch and getState functions, then assert which actions were dispatched.

Q: Can thunks access the store state? A: Yes. The second argument passed to every thunk function is getState, which returns the current state tree.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires