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.
1. Define Parameter in Payload Creator: The async function inside createAsyncThunk receives the parameter as its first argument.
2. Dispatch with Argument: When dispatching the thunk, pass the argument like a regular function call — e.g., dispatch(fetchUser(5)).
3. Use Parameter Inside Async Logic: Use the received parameter in API requests or any business logic.
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.