Redux is telling to use Redux Toolkit

Redux is telling to use Redux Toolkit

ยท

4 min read

What is Redux ToolKit? ๐Ÿ› ๏ธ

Introduction

The Redux Toolkit package was developed to be the new standard way to write Redux code, handling three major concerns about Redux itself...

  1. "Configuring a Redux store is too complicated"
  2. "I have to add a lot of packages to get Redux to do anything useful"
  3. "Redux requires too much boilerplate code"

These concerns were taken from and can be read further upon in the Redux ToolKit Documentation.

One important thing to take away is that Redux provides us with powerful data fetching and caching capability. This removes the need to create functions ourselves that would perform the same logic.

Using Redux Toolkit is not required when using Redux, however, it is encouraged because it does make your code DRYer and more maintainable while speeding up development. The package can be used at any skill level, and added at the beginning, in the middle, or at the end. I would encourage starting off your react/redux application with the redux toolkit package if you are planning to use it in the future, just to make things easier.

What's included inside Redux Toolkit?

Redux Toolkit includes the following APIs... These APIs were created to supply logic and avoid repetition.

  1. configureStore()

    • Wraps createStore to provide simplified configuration options and good defaults. It can automatically combine your slice reducers, adds whatever Redux middleware you supply, includes redux-thunk by default, and enables use of the Redux DevTools Extension.
  2. createReducer()

    • Lets you supply a lookup table of action types to case reducer functions, rather than writing switch statements. In addition, it automatically uses the immer library to let you write simpler immutable updates with normal mutative code, like state.todos[3].completed = true
  3. createAction()

    • Generates an action creator function for the given action type string. The function itself has toString() defined, so that it can be used in place of the type constant.
  4. createSlice

    • Accepts an object of reducer functions, a slice name, and an initial state value, and automatically generates a slice reducer with corresponding action creators and action types.
  5. createAsyncThunk

    • Accepts an action type string and a function that returns a promise, and generates a thunk that dispatches pending/fulfilled/rejected action types based on that promise.
  6. createEntityAdapter

    • Generates a set of reusable reducers and selectors to manage normalized data in the store.
  7. createSelector

    • Utility from the Reselect library, re-exported for ease of use.

RTK Query

The RTK Query is given as an optional addition to the Redux toolkit package. It was built to ease the work load for programmers, solving the use case of data fetching and caching. The RTK Query is a compact and powerful toolset used to define an API interface layer for your app.

The toolset is built on top of the Redux Toolkit, and uses Redux internally for its architecture. RTK query provides additional global store management capabilities. To further understand RTK query, it is recommended that you install the Redux DevTools browser extension. You can then examine and replay the behaviors of your requests and cache as they execute.

RTK Query is already included with Redux Toolkit package. You can simply add the code:

import { createApi } from '@reduxjs/toolkit/query'

/* React-specific entry point that automatically generates
   hooks corresponding to the defined endpoints */
import { createApi } from '@reduxjs/toolkit/query/react'

What does RTK Query include?

  1. createApi()

    • The core of RTK Query's functionality. It allows you to define a set of endpoints describe how to retrieve data from a series of endpoints, including configuration of how to fetch and transform that data. In most cases, you should use this once per app, with "one API slice per base URL" as a rule of thumb.
  2. fetchBaseQuery()

    • A small wrapper around fetch that aims to simplify requests. Intended as the recommended baseQuery to be used in createApi for the majority of users.
  3. ApiProvider

    • Can be used as a Provider if you do not already have a Redux store.
  4. setupListeners()

    • A utility used to enable refetchOnMount and refetchOnReconnect behaviors.

You can find more information about RTK query here in the Redux Toolkit documentation. But the main thing to take away from RTK query is that it...

  1. DRYs up your code.
  2. You avoid recreating logic, saving time and resources.
  3. It can create cleaner and more efficient code.

Conclusion

Redux Toolkit comes with a lot of tools that save you lines of code, time, and headaches. But its purpose and benefits have raised controversy in the tech realm. For some user's, the package is very useful and fits all the points listed above. However, some find that it requires a lot of boilerplate code and just makes things more confusing. The only way to truly find out, is to use Redux Toolkit for yourself! There are many tutorials out there explaining how to create small or large applications utilizing the toolkit. It is unknown whether Redux Toolkit is just another fad of programming, or here to stay...but we might as use while it's hot!

ย