0

In Redux’s createAsyncThunk I understand the second argument is an asynchronous payload creator. Please correct me if any of this is wrong. But if there’s any parameters, the arguments for async is supposed to be your param name and then ‘thunkAPI’, like so:

const fetchUserById = createAsyncThunk(‘users/fetchUserById’, async (**arg, 
thunkAPI**) => {//code}

If you pass in a parameter in for ‘arg’, do you always have to include ‘thunkAPI’ as the second argument? And if you don’t have a param you’re passing into async, you don’t have to include the thunkAPI or any argument in your async function (first example below)? Is that correct or am I way off? It seems like it follows this rule, but the third example I provided shows the async operation passing in a parameter (userId) and not including 'thunkAPI' which confuses me. I've been trying to look up documentation on it, but can't find concrete reasons when to include or not include thunkAPI depending on the params passed into the async operation.

Thank you very much for any help

//example given by my program with no arguments passed into async

export const loadRecipes = createAsyncThunk( "allRecipes/getAllRecipes", 
async () => { 
const data = await fetch("api/recipes?limit=10"); 
const json = await data.json(); 
return json; } );

//example given by my program with one argument passed into async and includes thunkAPI

const fetchUserById = createAsyncThunk( 'users/fetchUserById', async 
(**userId, thunkAPI**) => {
const response = await fetchUser(userId) 
return response.data } )

//example given by my program with one argument passed into async, but doesn't include thunkAPI

const fetchUserById = createAsyncThunk( 'users/fetchUserById', async 
(**userId**) => { 
const users = await fetch(api/users${userId}) 
const data = await users.json() 
return data } 

)

bandito24
  • 3
  • 2

0 Answers0