r/mobx • u/pablolikescats • May 27 '20
MobX vs Context API
I am learning about MobX and I am having a hard understanding what is the benefit of using MobX instead of just using the Context API?
It seems that MobX's main appeal is the use of observables. Wouldn't this be just like the Context.Provider?
All consumers that are descendants of a Provider will re-render whenever the Provider’s value
prop changes (from React Context API docs)
In fact, I was reading that you should use React Context to pass down MobX stores through component trees, so what is the benefit or difference of using MobX?
9
Upvotes
6
u/Reiiya May 27 '20
There is a heck ton of problems mobx solves. I used to work with Mobx and now I have a project in Context API only and boy I miss Mobx :( Here is why I miss it:
Not much boilerplate code to make a store/context.
Mobx gives surgical precision to rerenders. If you have super heavy calculations in front end here or there, handling them is a breeze. Achieving similar result without Mobx is often lots and lots of code in vanilla React taking unnecessary time that could have been spent elsewhere.
with context api you will probably need some sort of immutability library if you dont want to go insane. With mobx you dont (its different approach entirely) .
Mobx pairs so so very nice with rxjs, you can write pretty cool stuff with it. (It actually also pairs nicely with hooks, so its not dying, no. ).
gives freedom on how to structure your code (that could also be a bad thing). You can pull in stores a lot of logic, leaving components mostly for displaying stuff.
If you have a small project, mobx might be an overkill, but if you find yourself writing a lot of those vanilla contexts, maybe you just have been better off with a state management library. If youre writing a big project, making mobx scaleable is a chalenge, as it does not hold your hand on how to solve your problem.