07 / 18

7. How do you pass parameters to a thunk function?

Passing Parameters to Thunk Functions in Redux Toolkit

In Redux Toolkit, you can easily pass parameters to thunk functions created using createAsyncThunk. These parameters are received by the payload creator (the async function inside createAsyncThunk) and can be used for API calls, filtering, or conditional logic.

Steps to Pass Parameters
  1. 1

    1. Define Parameter in Payload Creator: The async function inside createAsyncThunk receives the parameter as its first argument.

  2. 2

    2. Dispatch with Argument: When dispatching the thunk, pass the argument like a regular function call — e.g., dispatch(fetchUser(5)).

  3. 3

    3. Use Parameter Inside Async Logic: Use the received parameter in API requests or any business logic.

Example: Passing Parameters to Thunk Function

You can pass any type of parameter — such as an ID, object, or filter — to dynamically control the async logic inside your thunk. This makes createAsyncThunk highly flexible for different API scenarios.