DEV Community

Discussion on: My thoughts on endless battle of React state management libraries (setState/useState vs Redux vs Mobx)

Collapse
 
rehubbard profile image
Eric Hubbard • Edited

"there is no easy way in React to share state instances between components"

React Context lets you easily share state in all components, doesn't it? This is a common solution to state management that uses built-in React features.

Collapse
 
krenwick profile image
Katrina

I made an account just to comment on this.
The whole time reading this article I was thinking "WHAT ABOUT USECONTEXT????"
This is the third article I've read today that discussed the struggle of shared state in react, and didn't mention the useContext hook. Is there something I'm missing?

Collapse
 
adamashored profile image
adam-ashored

No - it does not let you easily share state in all components. It let's you easily share state in all components that are descendents of the context provider.

Personally, I love context. It allows me to think of controllers as components. I don't have to deal with the boilerplate of redux and I don't have to deal with the magic of mobx.

But, if I'm on a team of developers that have various levels of experience in react, I would almost always go for redux as long as an experienced senior is driving the redux bus. If everybody on the team is experienced seniors in react, then context is a-ok by me.

Collapse
 
rehubbard profile image
Eric Hubbard • Edited

Having to be a descendant of a context provider isn't a huge limitation or drawback for context. But I'm sure there are some scenarios I could be missing where that's an issue.

Thread Thread
 
adamashored profile image
adam-ashored

I didn't say, nor imply, it was a drawback. It's not. It's actually a huge feature. React is still, even after several years of composable UI, a "new way of doing things" to a lot of devs and it's hard (or impossible) to grok it all at the outset. To say something like "context lets you share state across all components" is a confusing statement to the ones who haven't made their own mental models yet when they try it and it fails, or they nest context providers without realizing it and end up in a really uncomfortable state because they can't connect "context = global shared state" with what they're seeing with they're own eyes.

Thread Thread
 
rehubbard profile image
Eric Hubbard

Ah I gotcha. I read your original comment wrong. Thanks for clearing that up for other readers 👍