Mastering State Management: A Deep Dive into Redux Toolkit 🚀
Published: 2024-03-05 11:33:39


**Overview :**

State management is a critical aspect of building complex web applications. It enables developers to maintain and manipulate data across different components effectively. Redux, a predictable state container for JavaScript apps, has been a popular choice for managing application state in React applications.

However, setting up and maintaining Redux can be cumbersome, often involving writing boilerplate code.

Enter Redux Toolkit, a powerful library that simplifies the process of using Redux while also providing additional utilities to streamline development. In this blog post, we'll dive deep into Redux Toolkit, exploring its features, benefits, and how to effectively integrate it into your projects.

**What is Redux Toolkit?**

Redux Toolkit is an official package provided by the Redux team to simplify and improve the development experience when using Redux for state management in your React applications. It's a collection of utility functions, conventions, and best practices that make it easier to write and maintain Redux code. Redux Toolkit aims to streamline common Redux tasks, reduce boilerplate code, and encourage good practices.

### **Installing Redux Toolkit:**

`npm install @reduxjs/toolkit react-redux`

This will give you access to the React-Redux and Redux Toolkit packages. The next thing you will want to do is to create a ‘store.js’ file somewhere in your source folder.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709614683166/126e0225-fc77-4f93-8e47-189652daef06.png align="center")

Right now you can see that we have no reducers, but soon we will. First, we have to make sure our App can access the store, so we want to wrap our component inside of a component.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709614788206/2a268e77-d23e-4e9b-812e-ea77957a2283.png align="center")

Now that we’ve passed down ‘store’ as a prop and our app has access to this global state, we can set up our reducers to help us update that state.

### **createSlice**

Slices are a helpful update from the Redux Toolkit that lets us make multiple different stores as ‘slices’ of a whole store. We can put them all together, with their respective reducers, and make one global store, made up of many slices.

Let’s say we’re making a digital shopping app for our friend’s muffin store. Our friend wants us to keep track of all the muffins that a user clicks on, but also wants us to use LOTS of components. Luckily, Redux can help us keep track of all the muffins no matter which component our user is viewing.

Let’s start by creating a ‘muffinSlice’ reducer that will keep track of an initial state and any reducer functions we want to be available to us.

This is when we used vanilla redux :&gt;&gt;

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709615197291/adc2fd9e-7c2c-4773-b9b9-bc7fdc3044b0.png align="center")

We started out by importing our ‘createSlice’ API to gain access to the createSlice function. We then gave our slice a name (‘muffin’), an initial state (an empty array), and some reducers (‘addMuffin’ and ‘deleteMuffin’).

Normally, Redux requires us to use something called ‘Pure Functions’ when dealing with our reducers. This just means that the function should create a copy of the state and mutate that as opposed to directly messing with the state itself and possibly running into async issues.

Luckily, the ‘createSlice’ API gives us access to [Immer](https://immerjs.github.io/immer/), which converts our impure functions into pure functions. So let’s refactor:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709615248819/9902588e-28f8-4984-b565-31cb224db6a2.png align="center")

The (state, action) arguments allow us to have access to the state at that given time, plus any data we want to pass into our reducers through our action’s ‘payload’. Lastly, we can export our reducers and our slice to be used throughout our app.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709615441550/cb179d46-4d71-4916-aa5e-eb28f23ee84a.png align="center")

And we should also remember to update our ‘store.js’ file with our new reducer!

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709615461595/0582d802-6245-4577-ae47-dfc8b841991f.png align="center")

### **useSelector and useDispatch**

Now we’re set up! All that’s left to do is use our React-Redux hooks to both view and send actions to our store.

Let’s import ‘useSelector’ to view our store and ‘useDispatch’ will allow us to send out our actions. We also have to remember to import our reducer actions from the ‘muffinSlice.js’ file as well.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709615640359/ceeaad70-f390-4681-aedf-ed91007adeea.png align="center")

Here, we’re using ‘useSelector’ to grab the state that exists in our ‘store.js’ file under the name ‘muffin’. If we wanted to make a cupcake or a pie reducer as well we could add those to our ‘store.js’ and call ‘useSelector’ on them as well.

We also set up a constant called ‘dispatch’ which allows us to use our ‘addMuffin’ and ‘deleteMuffin’ actions in callback functions.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709615656992/43c687a3-0512-4291-bc09-3792e2d41b0b.png align="center")

Here you can see we’ve used our ‘muffinList’ constant to display a list of all our selected muffins to display to the user. We’ve also added an ‘onClick’ event listener, which dispatches our ‘addMuffin’ action that we imported to add the delicious blueberry muffin to our global state.

### **Conclusion**

Redux is a very helpful tool that just takes a little bit of setting up to get used to. This guide is for anyone who wants to dive into Redux without getting caught up in the semi-extensive setup. If you just follow along with the pictures and replace my ‘muffins’ with whatever you need to keep track of across your app, you’ll be in business.

Oh, and download the [Redux Dev Tools](https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=en) for your Google Chrome browser to make checking your store updates much easier.

![redux-flow.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1632622171968/NJPY087vD.png?auto=compress,format&format=webp align="left")