This error occurs when using Redux Toolkit's createAsyncThunk function with incorrect arguments. The createAsyncThunk API requires exactly two arguments: a string action type prefix and a payload creator function that returns a promise.
The createAsyncThunk function from Redux Toolkit is a utility for creating Redux thunks that handle asynchronous operations like API calls. It generates three action types automatically (pending, fulfilled, rejected) based on the string prefix you provide. The function expects two mandatory arguments: 1) a string action type prefix (e.g., "users/fetchById"), and 2) a payload creator function that returns a promise. When you call createAsyncThunk with incorrect argument types or missing arguments, Redux Toolkit throws this error to indicate the API contract has been violated.
Ensure you are calling createAsyncThunk with exactly two arguments in the correct order:
import { createAsyncThunk } from '@reduxjs/toolkit';
// Correct syntax:
const fetchUserById = createAsyncThunk(
'users/fetchById', // 1st arg: string action type prefix
async (userId, thunkAPI) => { // 2nd arg: payload creator function
const response = await fetch(`/api/users/${userId}`);
return await response.json();
}
);The first argument must be a string literal or string variable:
// Correct - string literal
const fetchUser = createAsyncThunk('users/fetch', async () => { /* ... */ });
// Correct - string variable
const actionPrefix = 'users/fetch';
const fetchUser = createAsyncThunk(actionPrefix, async () => { /* ... */ });
// INCORRECT - number, object, array, etc.
const fetchUser = createAsyncThunk(123, async () => { /* ... */ }); // Error
const fetchUser = createAsyncThunk({ type: 'users/fetch' }, async () => { /* ... */ }); // ErrorThe second argument must be a function that returns a promise:
// Correct - async function
const fetchUser = createAsyncThunk('users/fetch', async (userId) => {
const response = await fetch(`/api/users/${userId}`);
return response.json();
});
// Correct - function returning a promise
const fetchUser = createAsyncThunk('users/fetch', (userId) => {
return fetch(`/api/users/${userId}`).then(response => response.json());
});
// INCORRECT - not a function
const fetchUser = createAsyncThunk('users/fetch', fetch(`/api/users/1`)); // Error
const fetchUser = createAsyncThunk('users/fetch', null); // Error
const fetchUser = createAsyncThunk('users/fetch'); // Error - missing second argMake sure you are importing createAsyncThunk correctly from Redux Toolkit:
// Correct import for Redux Toolkit
import { createAsyncThunk } from '@reduxjs/toolkit';
// Not from 'redux' or 'redux-thunk' packagesCheck your package.json to ensure @reduxjs/toolkit is installed and at a compatible version (1.5.0+ for createAsyncThunk).
If using TypeScript, ensure you provide proper type annotations:
import { createAsyncThunk } from '@reduxjs/toolkit';
import type { AppDispatch, RootState } from './store';
interface User {
id: number;
name: string;
}
// With TypeScript types
const fetchUserById = createAsyncThunk<User, number, {
dispatch: AppDispatch;
state: RootState;
}>(
'users/fetchById',
async (userId, thunkAPI) => {
const response = await fetch(`/api/users/${userId}`);
return await response.json() as User;
}
);Make sure type arguments match the function signature.
Create a minimal async thunk to isolate the issue:
import { createAsyncThunk } from '@reduxjs/toolkit';
// Minimal working example
const testThunk = createAsyncThunk(
'test/action',
async () => {
return 'test data';
}
);
console.log(testThunk.pending('requestId')); // Should log pending action
console.log(testThunk.fulfilled('result', 'requestId')); // Should log fulfilled actionIf this works, compare with your original code to find differences.
The createAsyncThunk function internally creates three action creators: pending, fulfilled, and rejected. The string prefix you provide is used to generate these action types (e.g., "users/fetchById/pending"). The payload creator function receives two arguments: 1) the first argument passed when dispatching the thunk, and 2) a thunkAPI object containing { dispatch, getState, extra, requestId, signal }. For advanced use cases, you can pass an options object as a third argument to configure the thunk behavior, but this is optional. Common options include condition (to skip execution) and dispatchConditionRejection.
React Hook useCallback has a missing dependency: 'variable'. Either include it or remove the dependency array react-hooks/exhaustive-deps
React Hook useCallback has a missing dependency
Cannot use private fields in class components without TS support
Cannot use private fields in class components without TS support
Cannot destructure property 'xxx' of 'undefined'
Cannot destructure property of undefined when accessing props
useNavigate() may be used only in the context of a <Router> component.
useNavigate() may be used only in the context of a Router component
Cannot find module or its corresponding type declarations
How to fix "Cannot find module or type declarations" in Vite