So, you’re wondering if redux is necessary for React? Well, that’s actually a good question to be asking. The short answer is no, Redux is not necessary for React applications. However, there are always clear warning signs when you should be using a state management library like Redux and signs that you shouldn’t.
Below I am going to answer some of the most common questions regarding when you should and shouldn’t be using Redux with your React app. So without further ado, let’s jump right into it.
Why Not To Use Redux With React
Because not all apps need Redux! Before you decide what tools to use, it’s important to first understand the application you are building and what tools and technologies can help mitigate any potential scaling and efficiency issues you may face. Let’s start by addressing the situations you may not need React with Redux
1. Do I Need To Use Redux While Learning React?
Before we jump into when Redux is and isn’t necessary with React, I wanted to first answer this question. Is Redux really necessary when you are first learning React?
If you are just learning React, there is no reason for you to be using Redux. React by itself takes a while to master and you should learn how to build pure React apps before learning redux.
If you jump the gun and learn Redux with React, you are not going to fully grasp how React passes states around to other components. If you skip this learning process, it will be very hard to understand the problem Redux is aiming to fix.
So do yourself a favor and learn pure React first.
2. Do I Need To Use Redux For Small Projects?
Redux definitely isn’t required for small projects. Especially if you have a one-page application, a business website, or a web app with only a handful of components. The good news is you can always refactor a small project to start using Redux later. There is no point in over-engineering a project that doesn’t need to be.
As your project grows in size, you may notice that passing around state and props in your React app is getting confusing and or tedious. This is usually a good time to think about refactoring.
If you want to use some sort of state management in smaller projects, it might also be useful to utilize the Context API. While not a fully-fledged out solution like Redux, it can be beneficial for smaller applications where passing around state is a hassle, but there isn’t much to pass around.
Using the context API means you don’t have access to the redux dev tools or clean scalable storage space for your state. However, it is built in to React and is much more lightweight than Redux.
Why Use Redux with React?
1. Use Redux For Apps That Change State Often
I learned this the hard way when building my hush chat app. Even though my application was small, I had to pass state back and forth for each user sending and receiving messages in the group chat. This ended up being a really challenging thing to do with pure React. In fact, it was a really trippy problem because you had to show the UI from the perspective of each user but from a single React component. Making the passing of props for each user a difficult problem to solve.
This was actually the moment I understood the power of Redux. If you always had a centralized place to push and retrieve the state from, you wouldn’t have to worry about shuffling around the state from each component or passing around to and from its parents. It’s as easy as pushing the state you want to utilize to your redux store and retrieving it.
So hopefully, this is a lesson you can take with you. If you know from the start your frontend UI is going to have a lot of different state changes happening frequently, you are probably better off using Redux or, if a smaller project, the Context API.
2. Use Redux When Working In Teams
If you are going to have multiple people working on your web application, it also likely makes sense to use Redux. While redux can be a bit challenging to learn, it gives structure to your application. Your co-workers are not going to have to go through your code to see what state is available in the app and where it is stored.
Having a centralized point for state management makes it easier for both you and your teammates to review all your apps state quickly and easily. Saving you all a lot of time, effort, and unnecessary communication.
Summary: When Is Redux Necessary In React
Hopefully, this article has helped you understand that Redux is not always necessary with React. This is especially true if you are still learning and experimenting with React or your UI is small and uncomplicated.
As the complexity of your UI grows, so do the potential benefits that Redux can bring to the table. If you have a large application, are working with frequent state changes, or have multiple team members, Redux will likely make your life easier.
That’s pretty much everything! If you have any comments or notes to add, feel free to leave them below in the comments. I would be happy to address them.
If you are new to React and Redux and want to find a place to learn them, check out this course.