reduxjs /redux-thunk

Thunk middleware for Redux

★ 17,654⑂ 1,014👁 163⑃ 148● 217▤ 20
TypeScriptMITCreated 2015-07-13
More from this author
Download: v3.1.0v3.1.0 ↓3.1.02023-12-04
+0
Daily
17,654
stars
1,014
forks
163
watchers
148
prs
217
issues
20
releases

README

Redux Thunk

Thunk middleware for Redux. It allows writing functions with logic inside that can interact with a Redux store's dispatch and getState methods.

For complete usage instructions and useful patterns, see the Redux docs Writing Logic with Thunks page.

!GitHub Workflow Status



 Installation and Setup

 Redux Toolkit

If you're using our official Redux Toolkit package as recommended, there's nothing to install - RTK's configureStore API already adds the thunk middleware by default:

js
import { configureStore } from '@reduxjs/toolkit'

import todosReducer from './features/todos/todosSlice'
import filtersReducer from './features/filters/filtersSlice'

const store = configureStore({
  reducer: {
    todos: todosReducer,
    filters: filtersReducer,
  },
})

// The thunk middleware was automatically added


 Manual Setup

If you're using the basic Redux createStore API and need to set this up manually, first add the redux-thunk package:

sh
npm install redux-thunk

yarn add redux-thunk


The thunk middleware is a named export.


More Details: Importing the thunk middleware

If you're using ES modules:

js
import { thunk } from 'redux-thunk'


If you use Redux Thunk in a CommonJS environment:

js
const { thunk } = require('redux-thunk')




Then, to enable Redux Thunk, use
applyMiddleware():

js
import { createStore, applyMiddleware } from 'redux'
import { thunk } from 'redux-thunk'
import rootReducer from './reducers/index'

const store = createStore(rootReducer, applyMiddleware(thunk))


 Injecting a Custom Argument

Since 2.1.0, Redux Thunk supports injecting a custom argument into the thunk middleware. This is typically useful for cases like using an API service layer that could be swapped out for a mock service in tests.

For Redux Toolkit, the getDefaultMiddleware callback inside of configureStore lets you pass in a custom extraArgument:

js
import { configureStore } from '@reduxjs/toolkit'
import rootReducer from './reducer'
import { myCustomApiService } from './api'

const store = configureStore({
  reducer: rootReducer,
  middleware: getDefaultMiddleware =
    getDefaultMiddleware({
      thunk: {
        extraArgument: myCustomApiService,
      },
    }),
})

// later
function fetchUser(id) {
  // The extraArgument is the third arg for thunk functions
  return (dispatch, getState, api) = {
    // you can use api here
  }
}


If you need to pass in multiple values, combine them into a single object:

js
const store = configureStore({
  reducer: rootReducer,
  middleware: getDefaultMiddleware =
    getDefaultMiddleware({
      thunk: {
        extraArgument: {
          api: myCustomApiService,
          otherValue: 42,
        },
      },
    }),
})

// later
function fetchUser(id) {
  return (dispatch, getState, { api, otherValue }) = {
    // you can use api and something else here
  }
}


If you're setting up the store by hand, the named export withExtraArgument() function should be used to generate the correct thunk middleware

Top issues

Discussions

Latest release

v3.1.0 v3.1.0 2023-12-04
This major release:

- Updates the packaging for better ESM/CJS compatibility
- Changes the package to use named exports instead of a default export

This release has breaking changes.  (Note: this actually points to v3.1.0, which includes a hotfix that was meant for 3.0.0.)

This release is part of a wave of major versions of all the Redux packages: Redux Toolkit 2.0, Redux core 5.0, React-Redux 9.0, Reselect 5.0, and Redux Thunk 3.0.

For full details on all of the breaking changes and other significant changes to all of those packages, see the "Migrating to RTK 2.0 and Redux 5.0" m

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON