DEV Community

loading...
Cover image for What, Why, When Redux for state management.

What, Why, When Redux for state management.

siradji profile image Siradji Awoual ・3 min read

Redux, without saying, is the most popular state management library for React. But sometimes it comes off as intimidating to beginners, well for me, it did.

Last week, I was reading through YouTube video comments when I saw someone asking why one should use redux instead of context API. This person went on with how he heard context API + hooks will make redux obsolete. (Like seriously?)
I was surprised and confused at the same time. Where and how did he get that idea from? After snooping around a little, I found out that most beginners believed that context API will replace redux and that they should not bother learning it.

This inspired me to write about the significance of redux and why everyone in the react community MUST learn and master it.

If you happen to disagree with my reasons and perspectives, feel free to drop a comment below.

So here is WHAT, WHY, WHEN redux for state management.

What is redux?

Redux is an open-source JavaScript library for managing application state. It is most commonly used with libraries such as React or Angular for building user interfaces. Similar to Facebook's Flux architecture, it was created by Dan Abramov and Andrew Clark.

Why you should learn and use redux.

This is perhaps the most important part of this article. I'll be frank with you, redux is not always the best option. Sometimes, depending on what you're building, you are better off with context API.
To give you an example, last month, I built an app for a client. It's a very basic app with minimal actions and app-level states. I figured out that using redux for something of that size is a bit of an overkill.
T
In that same month, I built an app for another client — a co-working space booking and management system. This app has over 10 different app-level states and a lot of actions. Imagine using context API here. I would have had one of the ugliest looking app.js file.

What I am trying to say here is that, if you are building something small, with minimal app-level states, use context API and if it's something large with lots of shared state, redux is your best bet.

Another reason why you'd want to learn and master redux is the popularity. 9 out of 10 companies that use react use redux for state management. So if you're planning on working for a company with an existing codebase, learning redux is a MUST. Some would argue you can learn it on the job, but I like learning and knowing things beforehand.

Imagine you are assigned a ticket to find and fix a bug in your company's state infrastructure(based on redux), and you don't know anything's about redux. Will you tell your manager to assign you a different ticket because you thought redux will be going away?

Alt Text

The last point I want to talk about is testing. I am a strong believer in code testing, so this might come off a bit personal. It is much easier for me to test redux actions than the context API.
Do the math: Easier testing = fewer bugs = quality code.

When to use redux?

Another thing that keeps off beginners from using redux is the amount of boilerplate code you need to write to set up redux. For me, that is not something big. I mean I can create a generic boilerplate and save it as a GitHub gist and use it whenever setting up redux.

Use redux when you're building something large with lots of shared state.

I plan on writing a small tutorial on how to structure your redux store, how to use the new(not so new to be honest) redux hooks, and perhaps even build a small app to elaborate better. Let me know if that is something of interest to you.

Discussion (11)

pic
Editor guide
Collapse
markerikson profile image
Mark Erikson • Edited

Hi, I'm a Redux maintainer. A few quick thoughts:

First, I've written articles and discussed when and why it makes sense to use Redux, as well as how Redux compares to various other tools and options:

Second, "modern Redux" is considerably different than the Redux code people might have seen in the past. In particular, we now teach people to use our official Redux Toolkit package as the standard approach for writing Redux logic. It includes utilities to simplify several common Redux use cases, including store setup, defining reducers, immutable update logic, and even creating entire "slices" of state at once:

redux-toolkit.js.org

The React-Redux hooks API also makes it easier to use Redux in their React components.

Finally, we've added a "Redux Essentials" tutorial in our core docs, which teaches beginners "how to use Redux, the right way", using our latest recommended tools and practices like RTK and the React-Redux hooks:

redux.js.org/tutorials/essentials/...

I've also just published a new "Redux Fundamentals" tutorial that teaches "how Redux works, from the ground up". That tutorial is now live:

redux.js.org/tutorials/fundamental...

Collapse
siradji profile image
Siradji Awoual Author

I am honored to have a redux maintainer here. Thanks a lot for your input!

Collapse
chadalen profile image
Chad Alen • Edited

In my opinion if you’re going to use Redux use a Redux library that reduces Reduxes boilerplate. Redux by itself has a ton of boilerplate and looks intimidating. Checkout easy-peasy.now.sh/ its amazing. Works with Redux dev tools too.

Collapse
alexandrudanpop profile image
Alexandru-Dan Pop

RTK - redux toolkit - will also reduce some of the boilerplate.

Collapse
siradji profile image
Siradji Awoual Author

Never heard of that. I will defo check it out. Thanks!

Collapse
alexandrudanpop profile image
Collapse
siradji profile image
Siradji Awoual Author

That's one dope article! Well written.

Collapse
alexandrudanpop profile image
Collapse
laszland profile image
Laszlo Roland Kiss

Thank you for the great article. To me Redux was also a kind of pain in the ass, but I fought for it. I guess the hard part to teach/learn it, that you only see the benefit of Redux in case of a complex architecture, but you learn it always on easy projects. So I was also sceptical about it, why is it good to use? The point is that this is an important topic, looking forward to seeing your tutorial. 😊

Collapse
siradji profile image
Siradji Awoual Author

I am glad you liked it.

Collapse
imransheikh profile image
Imran-S-heikh

I know Redux...But using Recoil for small project.