Redux Toolkit

If you are a React Developer you have either used or heard of Redux. Redux became the most widely adopted state management tool for React, but has faced criticism along the way and has lost some popularity over the years. In complex, large applications you will likely have lots of components that use different pieces of state. Drilling your pops down levels and levels of your component tree gets messy fast. In comes Redux, a library used to extract your state from your components into a central store, which can be accessed anywhere in your application.

One of larger complaints of Redux, one that I have heard often, is “too much boilerplate code”. Redux takes a lot of hand written code to set up and effectively use it, which can push away a lot fo people. Creator of Redux, Dan Abramov, was aware of these concerns, which is why he and the Redux team built Redux Toolkit. Toolkit was created to address three main concerns of Redux.

  • “Configuring a Redux store is too complicated”

Redux Toolkit abstracts a lot of the Redux practices and simplifies the code for users. You can still implement the whole power of Redux, just faster and with less lines of code. Toolkit syntax is now recognized by Redux as the standard configuration for Redux whether you are just learning it or a Redux master.

Overview of what is included in Redux Toolkit:

  • 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.

(List from Redux.js.org)

Let’s take a look at an example of how configureStore API extracts logic and simplifies the store creation process in comparison to classic Redux.

from Redux.js.org

This is quite a bit of logic to set up the store, let’s look at what is going on.

We are

  • Using combineReducers to combine the slice reducers to create the root reducer

That’s a lot of code just to whip up a store, but not to fear, Redux Toolkit is here. The API, configureStore is a wrapper around the createStore API that simplifies this process way down, let’s see.

from Redux.js.org

Those two files of work was all extracted by the configureStore API.

Let’s break down what it did :

  • Combined our reducers with the root reducer function that handles our state and created the store with that reducer.

Redux Toolkit streamlines the way we write our Redux code and it is so smooth! In the next post I will cover more of the features

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store