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