What is Redux Toolkit? 🔨
Tags: react, redux, javascript, beginners
Published: 2021-07-07 15:34:57

##Table Of Contents
[Introduction](#1)
[What's included inside Redux Toolkit?](#2)
[RTK Query](#3)
[What does RTK Query include?](#4)
[Conclusion](#5)


## Introduction <a name="1"></a>

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](https://redux-toolkit.js.org/introduction/getting-started).* 

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? <a name="2"></a>

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

1. [configureStore()](https://redux-toolkit.js.org/api/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()](https://redux-toolkit.js.org/api/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()](https://redux-toolkit.js.org/api/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](https://redux-toolkit.js.org/api/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](https://redux-toolkit.js.org/api/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](https://redux-toolkit.js.org/api/createEntityAdapter)
  + *Generates a set of reusable reducers and selectors to manage normalized data in the store.*

7. [createSelector](https://redux-toolkit.js.org/api/createSelector)
  + *Utility from the Reselect library, re-exported for ease of use.*

## RTK Query <a name="3"></a>

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:

```javascript
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? <a name="4"></a>

1. [createApi()](https://redux-toolkit.js.org/rtk-query/api/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()](https://redux-toolkit.js.org/rtk-query/api/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](https://redux-toolkit.js.org/rtk-query/api/ApiProvider)
   + *Can be used as a Provider if you do not already have a Redux store.*

4. [setupListeners()](https://redux-toolkit.js.org/rtk-query/api/setupListeners)
    + *A utility used to enable refetchOnMount and refetchOnReconnect behaviors.*


You can find more information about [RTK query](https://redux-toolkit.js.org/rtk-query/overview) 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 <a name="5"></a>

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!