DEV Community

Discussion on: Using React Context API Like a Pro

Collapse
 
markerikson profile image
Mark Erikson

The answers are "no", but also "it's complicated".

First, it's important to understand that Context and Redux are very different tools that solve different problems, with some overlap.

Context is not a "state management" tool. It's a Dependency Injection mechanism, whose only purpose is to make a single value accessible to a nested tree of React components. It's up to you to decide what that value is, and how it's created. Typically, that's done using data from React component state, ie, useState and useReducer. So, you're actually doing all the "state management" yourself - Context just gives you a way to pass it down the tree.

Redux is a library and a pattern for separating your state update logic from the rest of your app, and making it easy to trace when/where/why/how your state has changed. It also gives your whole app the ability to access any piece of state in any component.

So, yes, you can use both of them to pass data down, but they're not the same thing. It's like asking "Can I replace a hammer with a screwdriver?". Well, no, they're different tools, and you use them to solve different problems.

Beyond that: Context has limitations on how state updates are propagated. In particular, every component that consumes a given context will be forced to re-render when that context value updates, even if the component only cares about part of the data inside the context value.

For more details, please see my posts:

Collapse
 
holdmypotion profile image
Rahul • Edited

Super informative. Thank you so much for the response!
"""
You're actually doing all the "state management" yourself - Context just gives you a way to pass it down the tree.

Redux is a library and a pattern for separating your state update logic from the rest of your app, and making it easy to trace when/where/why/how your state has changed. It also gives your whole app the ability to access any piece of state in any component.
"""
by- Mark Erikson

Collapse
 
baziotabeans profile image
fabioBaziota

Amazing you definitely clarified the difference...