React 16.3.0 was released and the Context API is not an experimental feature anymore. Dan Abramov (the creator of Redux) wrote a good comment here about this, but it was 2 years when Context was still an Experimental feature.
My question is, in your opinion/experience when should I use React Context over React Redux and vice versa?
As Context is no longer an experimental feature and you can use Context in your application directly and 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:
Redux
is much more powerful and provides a large number of features that theContext Api
doesn't provide, also as As @danAbramov mentionedIts upto 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 component, however application which use centralised data and handle API request in Action creators using
redux-thunk
orredux-saga
still would need redux. Apart from this redux has other libraries associated likeredux-persist
which allow you to save store data in localStorage and rehydrate on refresh which is what context API still doesn't support.As @dan_abramov mentioned in his blog You might not need Redux, that redux has useful application like
With these many application its far too soon to say that Redux will be replaced by the new Context API
The only reasons to use Redux for me are:
You probably don't need the level of indirection for your whole app, so it's fine to mix styles and use local state/context and Redux both at the same time.
From: When to use Redux?
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 the logic of your app outside of your components, keeping a state updates history, using Redux DevTools, Redux Undo, Redux Persist, Redux Form, Redux Saga, Redux Logger, etc…), then there is absolutely no reason for you to replace Redux with the
Context
API.References:
I prefer using redux with redux-thunk for making API calls (also using Axios) and dispatching the response to reducers. It is clean and easy to understand.
Context API is very specific to the react-redux part on how React components are connected to the store. For this, react-redux is good. But if you want to, since Context is officially supported, you could use the Context API instead of react-redux.
So, the question should be Context API vs react-redux, and not Context API vs redux. Also, the question is slightly opinionated. Since, I am familiar with react-redux and use it in all projects, I will continue to use it. (There is no incentive for me to change).
But if you are learning redux just today, and you have not used it anywhere, it is worth giving Context API a shot and replace react-redux with your custom Context API code. Maybe, it is much cleaner that way.
Personally, it is a question of familiarity. There is no clear reason to choose one over the other because they are equivalent. And internally, react-redux uses Context anyways.