This post was originally published at stevemerc.com
Reading some Redux example code I wrote four years ago and I have no effing idea what's going on there22:47 PM - 04 Nov 2019
(In case you're unaware, Dan co-created Redux.)
Ever since React's context API became stable, and especially after the release of hooks, a boatload of posts told us that Redux was on its deathbed. Who needs Redux when we've got the context API? Who wants to write all that boilerplate code? It's way too verbose when all I want to do is push an item to an array!
I don't necessarily disagree with those sentiments. Redux can be a pain to use, but sometimes it's the right choice for a project.
I want to pause here and say that I'm really grateful for Redux. Its creators and maintainers have spent a ton of time and energy in making a powerful tool. I've made good money working on projects that use it. And it's probably single handedly responsible for teaching more people how reduce
works than anything else.
So, Should You Learn Redux?
When a post asks if you should learn something, the answer is usually "it depends." But not this post. Yes, you should learn Redux.
Regardless of the flak it gets, it's still the most popular state management library for React apps. It's battle tested, has incredible tooling, and there's a large community around it. If you've got a question, someone's already answered it.
This means that you're likely going to encounter and work on a project that uses it. And no matter how much you protest that there's a better option (maybe there is, maybe there isn't), you're gonna have to use it. So, rather than fight it, embrace it.
The official docs are great. Spend some time with them. Redux isn't the easiest library to grok, which is why knowing how it works is useful before you find yourself neck deep working in a project that uses it.
I'd also recommend learning how Reselect and Immer work. They're not necessary to use Redux, but they can improve performance and readability, and it's not uncommon to see them used alongside Redux.
[Edit: also check out Redux Starter Kit, mentioned by Mark Erikson in the comments below.]
Should You Use Redux?
This is where I respond with "it depends". Personally, I've stopped using Redux for new projects. Local state and the context API can get you very far. And when I need something more powerful, I use MobX. The balance it strikes between Redux and not-Redux is great. But, as a consultant, I often find myself working on projects that use Redux, and knowing how it works is essential.
In the end, the choice to learn Redux and the choice to use it are two different things. I think it's at least worth learning.
đź‘‹ Enjoyed this post?
Join my newsletter and follow me on Twitter @mercatante for more content like this.
Top comments (17)
One thing I'd also add about redux is the amazing tooling via the Redux DevTools.
zalmoxisus / redux-devtools-extension
Redux DevTools extension.
Redux DevTools Extension
Installation
1. For Chrome
extension.zip
from last releases, unzip, openchrome://extensions
url and turn on developer mode from top left and then click; onLoad Unpacked
and select the extracted folder for usenpm i && npm run build:extension
and load the extension's folder./build/extension
;npm i && npm start
and load the extension's folder./dev
.2. For Firefox
npm i && npm run build:firefox
and load the extension's folder./build/firefox
(just select a file from inside the dir).3. For Electron
REDUX_DEVTOOLS
in electron-devtools-installer.4. For other browsers and non-browser environment
Usage
1. With Redux
1.1 Basic store
…
The Redux devtools is amazing. I have nothing but good things to say about it.
I think this is fitting as well
Ha! I was wondering if I should add that to this post when I was writing it. I could've used you as an editor!
But yes, Jani makes a great point.
Hi, I'm a Redux maintainer. I know you know my opinion already, but I wanted to pop in and offer my thoughts for anyone else reading this post.
First, related to the "Redux is dead" meme: I specifically addressed this in my Reactathon 2019 talk on "The State of Redux" and my post Redux - Not Dead Yet!. The TL;DR version is:
Specifically to the common question of "do hooks/context kill Redux?", see these additional articles:
I do think that almost everyone should learn Redux. Not only will you probably run across projects that use it, but understanding how it works and how to use it will definitely have a major effect on how you approach solving problems.
Per your comments on the docs and Reselect/Immer:
Folks should check out our new official Redux Starter Kit package. 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 without writing any action creators or action types by hand. It's powered by Immer inside, includes Reselect, and is our recommended default approach for writing Redux logic:
redux-starter-kit.js.org
And finally, we're starting a major rewrite of the Redux core docs. It'll be a long-term effort, but I think we can make the docs better and easier to understand.
Hopefully that helps add some clarity. If anyone has questions, ask away!
Hi Mark, thanks for chiming in!
^ A million times, this.
Thanks for mentioning Redux Starter Kit; I should've included that in this post.
What is a greenfield project? Also you suggest learning redux but you have stopped using it and now use mobx! It's kinda confusing where you want to direct your readers.
Sorry if it's confusing. I'm recommending that folks learn Redux, but not necessarily use it for their own projects (that's completely up for you to decide.) This is coming from the point of view that React developers are likely to work on projects that use Redux (think existing client work), so knowing how to use Redux is valuable knowledge.
Thanks, it would have been more clear if you had included this in blog as well. i am just learning react and find redux very complex so i don't wanna jump into it but every project i work on use redux so i have to learn it even though there are other options like you mentioned.
A greenfield project means a brand new project, as in not based on existing code.
Like you said, eventually, you might need to work on react+redux apps, so it's still a must to learn in my opinion for react devs out there. Context & hooks are still not as popular as redux/mobx in bigger companies who are already using react+redux/mobx. They won't change their existing large codebase because it's the new hottest thing. Maybe they will give it a shot on a greenfield, less impactful project.
Anyway, nice article! :)
I would recommend every developer to take a close look at redux and make an effort to really understand it. The library (i.e. the actual implementation) in and on itself is actually secondary in this case, but the concept (single-directional data flow + persistent data structures) is very powerful and a useful tool to have on your belt in many other cases, even outside UI and Javascript. I feel that I learned a lot about proper data flow by studying it, and I'm not even a real frontend developer.
Nice post! thanks for sharing it, i'm actually using react, redux and node.js in my actual job and i really enjoy a lot using redux and redux devtools.
Glad to hear you’re enjoying it. Have you tried Reselect or Immer with Redux?
Yes! i used reselect to optimize my React/Redux apps!
Why not? I there any better alternative? In my opinion the "hooks & context" solution doesn't help, it generates code mess.
"Better" is subjective per project. Sometimes Redux is the right choice, sometimes it's not. Hooks & context aren't messy in and of themselves; they're tools (just like Redux, MobX, etc.)