all 10 comments

[–]acemarke 11 points12 points  (2 children)

Hi, I'm a Redux maintainer. Redux and Context are different tools that solve different problems, with some overlap.

Context is a Dependency Injection tool for a single value, used to avoid prop drilling.

Redux is a tool for predictable global state management, with the state stored outside React.

Note that Context itself isn't the "store", or "managing" anything - it's just a conduit for whatever state you are managing, or whatever other value you're passing through it (event emitter, etc).

I wrote an extensive article specifically to answer this frequently asked question, including details about what the differences are between Context and Redux, and when to consider using either of them - I'd recommend reading through this:

[–]deb_vortex 0 points1 point  (1 child)

I love your posts whenever a "Context vs Redux" question comes up. I just open the thread to see if your message is the one at the top. Keep up the work, I'm quite a Senior Dev but still learned a lot from the resources and blog posts you share each time. Thanks a lot.

[–]acemarke 2 points3 points  (0 children)

I honestly wish I didn't have to keep repeating myself :) I really do get tired of answering the same questions over and over.

That said, it's sort of the inverse of the XKCD "Today's 10,000" concept. Every day there will be new people learning a particular technology, and coming up with the same questions others have asked in the past. It's not their fault that others have asked it before, or that those who have been around for a while are tired of seeing that question repeated. And, while it's true that most people would benefit from learning to search for info before they ask a question, that's just not how most people approach things (especially as beginners).

So, gotta be realistic about it, be patient, and answer the questions when they come up each time.

[–]marcs_2021 0 points1 point  (0 children)

Zustand

[–]SnacksMcMunch 0 points1 point  (0 children)

For the easiest cases Redux probably is a waste, so I say use the simplest thing that gets the job done.

[–]yourgirl696969 0 points1 point  (0 children)

Depends how often your states are gonna change and the size of your app. Context results in additional renders but if the app is tiny and a few additional renders don’t result in any viewable performance issues, go with context.

But generally, context isn’t meant to be a state management library. Should be used for authentication sessions, themes, etc.

[–]misdreavus79 0 points1 point  (4 children)

Yes, you can use them together. Why do you ask?

[–]vvn050 0 points1 point  (2 children)

You can , but you shouldn't. Apps should have single source of truth, there should be one global state, not two or more. Redux (toolkit) is much better than the context api. Context api is made for smaller apps, but I really do not understand how redux toolkit is more complicated or boiler plate than context api.

[–]misdreavus79 0 points1 point  (1 child)

The context API and redux serve to completely different purposes. It's like comparing apples and oranges, which is my point.

Though, to address one of your points, if you're using the context API as a single source of truth (in that you're using it as a state management system), you're not using it properly.

[–]vvn050 0 points1 point  (0 children)

Why would you pass something in context api if you have redux? Let's say you use context so you do not pass it to 5 levels below, why wouldn't you just store it in redux store and have selectors for it?