From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Explanation of code with async thunk and slice

Explanation of code with async thunk and slice

Welcome back. In this video, we are going to see an example of createAsyncThunk. That's how a createAsyncThunk usage look like. You have to import it from ReduxJS toolkit library. Then you get to have a function from this. It returns back a function. In this case, we have named it as fetch users. After that, you have to define an action type which is users-fetch and then you have to call an async function. This is an anonymous async function. Inside that function, we are using either fetch which is by default in javascript for calling external APIs, and we can also have XEOs installed and we can call XEOs here too. Inside the fetch, we can place the path of our API. In this case, it is for fetching the list of users. And then while awaiting this async function, we can return back the response that we get. Let's take a look on the slice and the other concepts of RTK works with async approaches. We have to create a slice, which we were doing in the previous codebase example. We have to…

Contents