The key reason to use createAsyncThunk is that it generates actions for each of the different outcomes for any promised-based async call: pending
, fulfilled
, and rejected
. We then have to use the builder callback API on the extraReducers property to map these actions back into reducer methods we then use to update our state. It's a bit of of a process but it's simpler than the alternative, which is to create a bunch of actions by hand and manually dispatch them.
createAsyncThunk takes a type
argument (which I referred to as a label) and a function called a payloadCreator
which is basically just an async function (or function returning a promise) that goes and collects data.
The builder API is one way to define extraReducers
(you can also pass in an object with a string). It's semantically similar to a switch statement and includes methods like addCase
and addDefaultCase
.
Hmmm, Code not working ((