Redux toolkit with async API call using createAsyncThunk

In this redux toolkit article, we will show you how to create an async API call using createAsyncThunk action. In this example we will use Axios package for API call.

Demo Application

Redux toolkit with async API call using createAsyncThunk - Clue Mediator
Output – Redux toolkit with async API call using createAsyncThunk – Clue Mediator

Redux toolkit with async API example

  1. Create a react app
  2. Install dependencies
  3. Configure the store using redux toolkit
  4. Create a user actions
  5. Add action to userSlice
  6. Call API action from react component
  7. Output

1. Create a react app

First, Create a react application using create-react-app. Run the following command to create an application.

We will refer the following project structure to create an example.

  • redux-toolkit-api-example
    • node_modules
    • public
      • index.html
    • src
      • store
        • user
          • actions.js
          • userSlice.js
        • index.js
      • App.js
      • index.css
      • index.js
    • package-lock.json
    • package.json
    • README.md

2. Install dependencies

Add the Redux Toolkit and React-Redux packages to your project. Also install the Axios to hit the API. Run the following command to install the packages.

You may see the following versions in the package.json file.

react
^18.2.0
@reduxjs/toolkit
^1.9.1
react-redux
^8.0.5
axios
^1.2.0

3. Configure the store using redux toolkit

Now, we have to configure the store using redux toolkit. Refer to the following article for more information.

How to set up and use Redux Toolkit with React-Redux

4. Create a user actions

In this example, we will create a file named actions.js under src/store/user directory. In this file, we have to write an API to get the data using createAsyncThunk method.

A createAsyncThunk method that takes a string representing a Redux action type and a callback function that should produce a promise. Based on the action type prefix that you supply, it generates promise lifecycle action types and delivers a thunk action creator that will execute the promise callback and dispatch the lifecycle actions in accordance with the delivered promise.

src/store/user/actions.js

5. Add action to userSlice

Let’s create a userSlice.js in the same directory and add the above action in the 1extraReducers`.

src/store/user/userSlice.js

In the above code, you can see we have used three different actions of an API to update the store information.

6. Call API action from react component

At last, we have to design a react component to get the user data via API call on button click. So, we have added two buttons to get the next/previous page data and display on page.

src/App.js

7. Output

Run the application and check the output in the browser.

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

Demo & Source Code

GitHub Repository
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *