1. 22
    Accessing Global State inside of Async Thunks with TypeScript
    1m 21s

Accessing Global State inside of Async Thunks with TypeScript

Jamund Ferguson
InstructorJamund Ferguson
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

The payloadCreator argument to createAsyncThunk takes two arguments. The first is a single arg that receives data passed into the thunk's action creator such as dispatch(checkout(items)). The second is called thunkAPI that contains something like 7 different methods on it. Here we're going to use its getState() method to access the global state just before we make complete the checkout API call.

When it comes to typing createAsyncThunk just like other RTK methods, it will mostly be typed by default. In this case we still need to tell it about our store. We can do that with thunkAPI.getState() as RootState which I think is the preferred way to handle the types here. If we really want to be thorough though, we can pass in the full types like this:

createAsyncThunk<
  PayloadResponse,
  ThunkArgument,
  { state: RootState }
>("slice/typeName", payloadCreator)

Jamund Ferguson: [0:00] Open up cartSlice.ts and head into the createAsyncThunk() method. I'm going to rename the argument CartItems to _ and add a second argument. This one's called thunkAPI.

[0:09] In the first line of our function let's type const state = thunkAPI.getState(), and below that const items = state.cart.items. Then in Cart.tsx, when we call checkoutCart(), we don't need to pass in the items any longer. We're pulling it directly from the current state.

[0:28] The only problem with this approach is that TypeScript doesn't understand what's happening. In order to type this properly, we can say here const state = getState() as RootState, which will correct the type issue, or we can fully type this createAsynkThunk() method using the generic syntax here.

[0:47] First, it needs to know the checkout response, which in this case is going to be success: boolean. Then, it wants to know the type of your first argument. In this case, that would be our _ argument, which is currently undefined because we're no longer passing the cart items.

[1:00] The third argument here is going to be an object with the property of state, and that is going to be of type RootState.

[1:06] It looks something like this when it's all typed out, but with that, we will have full type support by TypeScript. This works fine, but it's easier just to tell it that state is RootState and then all of our types work perfectly.

egghead
egghead
~ 12 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today