I don't have the experience of working on a massive sized projects. The small to medium ones that I have worked one, I kinda didn't feel the necessity of Redux or any other state management tools. Also the usecases I have seen for Redux or the places where I have used Redux, those can be done with context as well. So my question is where exactly do I need Redux and what does it provide that can't be handled by context and other hooks? Also does a state management tool provide improved performance compared to context?
As Context is no longer an experimental feature and you can use Context in your application directly and it is going to be great for passing down data to deeply nested components which what it was designed for.
As Mark Erikson has written in his blog:
If you're only using Redux to avoid passing down props, context could replace Redux - but then you probably didn't need Redux in the first place.
Context also doesn't give you anything like the
Redux DevTools, the ability to trace your state updates,middlewareto add centralized application logic, and other powerful capabilities thatReduxenables.
Redux is much more powerful and provides a large number of features that the Context API doesn't provide, also as @danAbramov mentioned
React Redux uses context internally but it doesn’t expose this fact in the public API. So you should feel much safer using context via React Redux than directly because if it changes, the burden of updating the code will be on React Redux and not you.
Its up to Redux to actually update its implementation to adhere with the latest Context API.
The latest Context API can be used for Applications where you would simply be using Redux to pass data between components, however applications which use centralised data and handle API request in Action creators using redux-thunk or redux-saga still would need Redux. Apart from this Redux has other libraries associated with it like redux-persist which allows you to save/store data in localStorage and rehydrate on refresh which is what the Context API still doesn't support.
As @dan_abramov mentioned in his blog You might not need Redux, Redux has useful applications like
- Persist state to a local storage and then boot up from it, out of the box.
- Pre-fill state on the server, send it to the client in HTML, and boot up from it, out of the box.
- Serialize user actions and attach them, together with a state snapshot, to automated bug reports, so that the product developers
can replay them to reproduce the errors.- Pass action objects over the network to implement collaborative environments without dramatic changes to how the code is written.
- Maintain an undo history or implement optimistic mutations without dramatic changes to how the code is written.
- Travel between the state history in development, and re-evaluate > the current state from the action history when the code changes, ala TDD.
- Provide full inspection and control capabilities to the development tooling so that product developers can build custom tools for their apps.
- Provide alternative UIs while reusing most of the business logic.
With these many applications its far too soon to say that Redux will be replaced by the new Context API.
If you are using Redux only to avoid passing props down to deeply nested components, then you could replace Redux with the Context API. It is exactly intended for this use case.
On the other hand, if you are using Redux for everything else (having a predictable state container, handling your application's logic outside of your components, centralizing your application's state, using Redux DevTools to track when, where, why, and how your application's state changed, or using plugins such as Redux Form, Redux Saga, Redux Undo, Redux Persist, Redux Logger, etc…), then there is absolutely no reason for you to abandon Redux. The Context API doesn't provide any of this.
And I personally believe that the Redux DevTools extension is an amazing, underestimated debugging tool, which justifies by itself to keep using Redux.
Some references:
- Redux Is Not Dead Yet!
- You Might Not Need Redux
- Do React Hooks Replace Redux?
Redux or React Query or Context API
Redux vs Context API
Does Context is intended to replace or to not need Redux?
I was on a similar reddit post not too long ago and had the assumption beforehand that react context was a state management system, which it is not. Someone linked this article to me that was super helpful. https://blog.isquaredsoftware.com/2021/01/context-redux-differences/
tldr: context is really just a way to avoid dealing with passing props through nested components. You'd still keep track of state with React hooks like useState, then just pass that state through context. That article goes way deep into why it isn't a redux replacement, too.
Hope it helps!
More on reddit.comusecontext vs redux
They are both different. Context isn’t great for general state management. It’s for specific use cases where you need contextual state for a subtree of components and each instantiation should have its own state.
Redux is for global application state.
But to be honest the API of either one are not my favourite. For most cases I’d use Zustand and maybe context in some specific use cases. It’s pretty rare context is actually required though.
More on reddit.com