Actions in Redux Toolkit

Actions are a fundamental concept in Redux Toolkit that represent the events that occur in your application. They serve as a way to update the state of your application by dispatching them to the Redux store.

In Redux Toolkit, creating actions is made easier and more efficient with the use of the createSlice and createAsyncThunk utilities. Let’s take a closer look at these two ways of defining actions in Redux Toolkit.

1. createSlice

The createSlice function is a powerful utility that helps generate action creators and reducers. It combines the process of creating actions and reducers into a single step, reducing boilerplate code. Here’s an example of how to use createSlice:

import { createSlice } from '@reduxjs/toolkit';

const counterSlice = createSlice({
  name: 'counter',
  initialState: 0,
  reducers: {
    increment: (state) => state + 1,
    decrement: (state) => state - 1,
    reset: (state) => 0,
  },
});

export const { increment, decrement, reset } = counterSlice.actions;
export default counterSlice.reducer;

In the example above, we define a counter slice using createSlice. It generates the necessary action creators (increment, decrement, and reset) and a reducer function. These action creators can be dispatched to update the state of the counter slice.

2. createAsyncThunk

createAsyncThunk is another utility provided by Redux Toolkit that simplifies handling asynchronous actions in Redux. It generates an action creator that dispatches multiple actions to handle different stages of an async operation (e.g., loading, success, and error). Here’s an example of how to use createAsyncThunk:

import { createAsyncThunk } from '@reduxjs/toolkit';
import { fetchUser } from '../api/userApi';

export const fetchUserById = createAsyncThunk(
  'users/fetchUserById',
  async (userId) => {
    const response = await fetchUser(userId);
    return response.data;
  }
);

In the example above, we define an fetchUserById async thunk using createAsyncThunk. It generates an action creator that handles fetching a user by their ID. The async function passed as the second argument performs the asynchronous operation (e.g., making an API call) and returns the data.

Using createSlice and createAsyncThunk from Redux Toolkit simplifies the process of defining actions and handling asynchronous operations in Redux. These utilities eliminate the need to write boilerplate code, making your codebase more concise and efficient.

#redux #reduxtoolkit