How to fix createAsyncThunk requires a string action type and a payloadCreator function in React
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.
Quick fix
Fixes createAsyncThunk requires a string action type and a payloadCreator function
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();
}
);- Verify the basic createAsyncThunk syntax
- Check the action type prefix is a string
- Ensure the payload creator is a function
- Review imports and package version
import { createAsyncThunk } from '@reduxjs/toolkit';// Correct syntax:const fetchUserById = createAsyncThunk('users/fetchById', // 1st arg: string action type prefixasync (userId, thunkAPI) => { // 2nd arg: payload creator functionconst response = await fetch(`/api/users/${userId}`);return await response.json();});createAsyncThunk requires a string action type and a payloadCreator function
What this error means
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.
Typical symptoms
- Redux thunk creation fails during development
- Console shows error: "createAsyncThunk requires a string action type and a payloadCreator function"
- Application crashes when trying to create an async thunk
- TypeScript may show type errors if using incorrect argument types
- Redux DevTools shows no async actions being dispatched
Common causes
- Passing a non-string value as the first argument (action type prefix)
- Forgetting to provide the payload creator function as the second argument
- Passing undefined or null as either argument
- Providing additional arguments beyond the two required ones
- Using an object or array instead of a string for the action type prefix
- Passing the payload creator function as the first argument instead of the second
- Importing createAsyncThunk incorrectly or from the wrong package
How to fix "createAsyncThunk requires a string action type and a payloadCreator function"
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.
Advanced notes
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.
Frequently asked questions
- What does "createAsyncThunk requires a string action type and a payloadCreator fu" mean?
- 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. , "users/fetchById"), and 2) a payload creator function that returns a promise.
- How do I fix "createAsyncThunk requires a string action type and a payloadCreator fu"?
- 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.
- What causes "createAsyncThunk requires a string action type and a payloadCreator fu"?
- Passing a non-string value as the first argument (action type prefix) Forgetting to provide the payload creator function as the second argument Passing undefined or null as either argument
- Is the "createAsyncThunk requires a string action type and a payloadCreator fu" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.