ribas89
ribas89

ribas89

React Context vs Redux

Which one is better?

ribas89's photo
ribas89
·Jan 13, 2022·

2 min read

React Context if you want to expose variables from a parent component to a child component.

Redux if you need more control over the variable changes, have to use between components outside a parent/child relationship or frameworks outside the react environment.

Let me explain why.

React Context is just a bridge between a component and its children. Do you want to modify a parent state? Change some theme variable or share some parent information with its children? Use React Context.

Redux is an entirely separated entity. Even if you integrate it in your react application, redux is on another scope within your javascript environment. On the other hand, React Context is entirely inside the react environment.

That makes Redux more flexible than React Context when you need to share the state between react and non react environments. Just get the store instance, and you have tracked all the mutations in your states.

Besides, Redux gives you control over who and when the mutations happened. When you need consistency in your values, redux is king.

In conclusion, go with React Context if you need to share a boolean, string, or another control variable between a parent component and its children. Go with redux if you need to store meaningful data and share this data asynchronously between components and environments.

Bonus: Route params

You can also share data between screens using Route Params. I like to use this feature when I need direct access to a screen from different sources or when the current screen's input data somehow feeds the next screen. I could use Redux or Context, but for me, Route Params is easier to maintain in those cases.

 
Share this