DEV Community

Cover image for What is Redux Toolkit? 🔨
Maxine
Maxine

Posted on • Updated on

What is Redux Toolkit? 🔨

Table Of Contents

Introduction
What's included inside Redux Toolkit?
RTK Query
What does RTK Query include?
Conclusion

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'

Enter fullscreen mode Exit fullscreen mode

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!

Top comments (16)

Collapse
 
elvis2280 profile image
Elvis2280

Hi, I’m learning React and I know react class/function components hooks, custom hook etc, then I guess is time to learn redux,
What do u recommend me start learning redux or redux toolkit

Btw I’m learning first react-query than redux idk if it’s a good idea 😅 is hard know the next step in the react journey

Ty for your post it was useful :)!! If you habe time check my second react app I created, is basic but it let me get solid my basic react knowledge
covid19-dashboard-elvis2280.netlif...

Collapse
 
maxinejs profile image
Maxine

Hi! Thank you so much for the feedback, it is greatly appreciated.

In regards to what order and how/what you should learn, I do not think there is a "wrong" way/order to do it. It is great you are familiar with React and what can be done with it! If you look in the Redux Toolkit documentation, they actually say they intended for Redux Toolkit to become the new standard when writing Redux logic, a lot of people are digging it!

I would say just go for Redux Toolkit, if you are interested in vanilla Redux, it is always something you can go back and read up on later. RTK allows you to do all kinds of things in less, more organized, code. Just have some patience while learning it, don't be afraid of the documentation, and you'll be A-okay!

Collapse
 
elvis2280 profile image
Elvis2280

Thank u so much, I really appreciate your help :), sorry for take your time but you think I can use nextjs without learn redux first, I wanna start developing my portfolio but I don’t know if U should first learn redux. Btw thank u for everything, happy code ;)

Thread Thread
 
maxinejs profile image
Maxine

No problem!

I think you can learn Next.js without going too deep into Redux, but Next.js uses Redux for state management. So that means, Redux's "store" is used to hold information about the state of the application.

If you are unfamiliar with how an application's state and store works, I would read up on the documentation (the Redux documentation is not that scary, I promise.) Happy coding to you too! Best of luck.

Collapse
 
lexiebkm profile image
Alexander B.K.

People should learn fundamentals of Redux first, gain a good grasp of its fundamental concepts like Store, Action, Action Creator, Dispatch and Reducer. Then, they can proceed to RTK to help working with Redux easier.

Collapse
 
icecoffee profile image
Atulit Anand

I also learned redux a couple of days back. It's easy then flux I must say. I liked it

Collapse
 
maxinejs profile image
Maxine

I have not yet used Flux, but good to know I started with the better one haha

Collapse
 
lexiebkm profile image
Alexander B.K.

As for React-Redux, when we are still using class components, then the only way to use React-Redux is by using Connect API, right ? Only when we use Hooks, then we can use Hooks API provided by React-Redux such as useSelector, useDispatch.
It seems that I need to get used to both ways, in anticipating (existing) codebases which use both class components and function components (with Hooks).
I myself have been only using class components in my last project. However, after reading motivation in React official site about Hooks, it's time for me to switch to functional components with Hooks. One thing I should consider when using Hooks, though, is whether 3rd party libraries I have been using, support Hooks in their APIs.
Sometimes, because of this different ways of using class components or Hooks in React, make me think to switch to Angular or Vue which I am still learning too.

Collapse
 
timminata profile image
Timothy Trewartha

Would you recommend porting a large code base using vanilla redux to redux-toolkit? We have typed all our reducer states and actions.

Collapse
 
maxinejs profile image
Maxine

I really don't think it matters what point you decide to transition from vanilla redux to redux-toolkit. Of course it's easiest to initially write all your code using the toolkit if you are planning on implementing later, just to save time and some headaches.

BUT, I also think that by writing you code in vanilla redux first, and then switching over can be very beneficial if you are newer to the react/redux realm. I've found there are ton of different ways to utilize Redux toolkit and you will come across tutorials and documentations showing it's versatility. Understanding your application, your reducers, actions, etc. will help you pick apart other code you may be learning from, so you're not just pattern following without understanding what's really going on. If you're really nervous about breaking your code, clone your repo, and try it out there first!

tldr, go for it. The worst that could happen is you'll learn something new.

Collapse
 
maxinejs profile image
Maxine

To add to that, once you start getting the hang of toolkit and the APIs supplied, it gets to be a lot fun and you can definitely do more with the application.

Collapse
 
maxinejs profile image
Maxine

@phryneas I have some questions about updating state useReducer. Do you think you could help?

Collapse
 
phryneas profile image
Lenz Weber

I'd recommend going on the Reactiflux discord: reactiflux.com/ There are always people to help there.

Collapse
 
maxinejs profile image
Maxine

Thank you so much!!!

Collapse
 
angerback profile image
Sebastián Meneses

Very informative and concise. Appreciate when everything is so clear, as it gave me a pretty good overview of the tool.

Thanks!!

Collapse
 
maxinejs profile image
Maxine

Thank you! I hope to be posting more articles soon, have been very busy with work (which is a blessing!)