DEV Community

Jesco Wuester
Jesco Wuester

Posted on

What are some real-world scenarios where redux is needed?

I'm struggling to see how redux is helpful. In pretty much every case state should be controlled by its own component and not made global (like redux does). There may be some exceptions like a user or shopping cart object or sth like that, but those can be solved with just one context provider.
Has anyone worked on a codebase (or does anyone know of an example) that really needs redux?

Top comments (7)

Collapse
 
drmandible profile image
DrMandible • Edited

Another user mentioned redux as a tool for keeping state logic organized rather than dispersed throughout the app. I think that's probably its biggest selling point.

That said, I'm personally at a loss to understand the benefits of redux over the gold standard of state management, finite state machines and state charts. I reach for the xstate library these days.

Edit: Turns out another dev user (dever? deviant? devmeister?) is doing a tutorial on them right now: dev.to/codingdive/introducing-stat...

Collapse
 
fly profile image
joon

Here's the scenario when I realized that I needed to study and apply redux.

I was working on a react project - it was actually my first - and at some point in an absolute maze of components, I realized there was a need to throw a certain stateful value from a component to another component, which was 5 components up the tree and 4 down.

Eventually I ended up throwing the value up the project tree until I did exactly that.

Now if I knew how to use redux, it would have been almost as easy as declaring a global variable(in some sense)

Of course using redux in all these cases is not an optimal design pattern and taking into account of these cases before you begin a project is a better approach.

But that was when the need occurred for me in my case.

The usual use case would be (in my experience) keeping user tokens/credentials, pretty much any dynamic value that needs to be accessed in most parts of the project.

And I don't mean to refute your statement that context provider will do the trick - it's true.

I believe the true value of redux is shown in terms of DX(Developer eXperience). The debugging tools are far superior to any other state management tools I've heard of. Which is probably why it is so widely used.

Trust me, reduxing pays off Jesco :)

Collapse
 
jsco profile image
Jesco Wuester

Interesting. I get the use case that you described above but I think in most cases that can be solved by structuring your components in a somewhat nicer way :D.
In my personal opinion, pure context has better dx than redux. I usually declare a provider like userProvider and a hook like useUser and then its pretty easy to consume the context.
I did use redux for nearly a year and in most cases it was just a burden, it came in handy once when I made a online photo editor but I figured that that's hardly a common use case :D

Collapse
 
fly profile image
joon • Edited

When I was refering to DX, I believe I might not have been specific enough. Yes I agree setting up redux is a pain and definitely has a steeper learning curve. And in that sense the DX is worse. The DX that I was referencing to was debugging code when projects get larger.
It can get increasingly difficult to do so as projects begin to get too large to even trace what's going on in where - especially when multiple developers are working on a single project.
In this scenario, redux is a god-send and I believe it has saved me countless hours of debugging because of redux's robust features when it comes to logging and debugging.
In small projects, I couldn't agree more that redux can feel a bit bloaty :)

Collapse
 
josemunoz profile image
José Muñoz

One of the best examples of Redux I've worked on was on a project that had a customization dashboard with a ton of widget options, you could create your own dashboard with your favourite widgets to visualize data. In this scenario all widgets connected to Redux to take the data they required to represent a visualization, this was very helpful on a large project like this, developing new features and maintaining all the widgets in sync would have been exponentially harder without it.

Collapse
 
fly profile image
joon

2cents worth a million.
👏

Collapse
 
benghamine profile image
Benjamin Dunphy

Lots of good discussion here. Mark Erikson (Redux maintainer) addressed this in his talk at Reactathon 2019 - youtube.com/watch?v=mtjHxwUQUs0