0

I'm new in redux toolkit and I've managed state with redux toolkit lately. But the thing I don't know, what is usage of first argument of CreateAsyncThunk. I've read this article: https://redux-toolkit.js.org/api/createAsyncThunk and according to this, CreateAsyncThunk has two argument and first argument is named type :

A string that will be used to generate additional Redux action type constants, representing the lifecycle of an async request

Ok. But we never need to call or use this argument again, so why is important to name this argument? I tried adsfds insted of requestStatus after / and my project worked perfectly! I also understand it also works even without slash.

It seems it doesn't matter what you write as first argument, It always works! So what is the usage of the first argument?

bami
  • 211
  • 1
  • 6
  • 19

1 Answers1

2

In Redux, every action is identified by a unique type string. So createAsyncThunk creates three actions for you - in your case with the type strings "adsfds/pending", "adsfds/fulfilled" and "adsfds/rejected".

If you do not use "asdfds" in any other createAsyncThunk, that's a perfectly fine thing to do, but if you look at the Redux Devtools browser extension to see what is happening in your application, a string like that might make it very difficult to read.

phry
  • 35,762
  • 5
  • 67
  • 81
  • Tnx, So the first argument is only for identifying by redux and recognized by Redux Devtools and there would be no problem for developer when not using a unique type in any other createAsyncThunk, right? – bami Aug 14 '22 at 15:44
  • 1
    Not really, but in the same way as you can also call your functions `ffsdnfkj323` and `fsoo2p3i42`, it won't help your code readability. – phry Aug 14 '22 at 17:51
  • 1
    Wait... rereading "when not using" and not 100% on how you structured that sentence, so I'll clarify: if you use the same type in multiple thunks, every of those thunks will trigger any `extraReducer` for any of the other thunks. These need to be unique. – phry Aug 14 '22 at 17:53
  • sorry. `when not using unique type` was my mistake, I meant `using unique type`! – bami Aug 15 '22 at 07:42