by shermanmccoy · 6y ago · 0 comments
I've found using React context for state, and judicious usage of useMemo has been enough for me to be able to drop mobx or its kin, even on larger sized projects.
Libraries like mobx and redux implicitly promote a massive singleton state object. I think web applications are easier to maintain if we make every effort to minimise global state. If some component way down on the left hand side of the tree needs to get something to a component at say the top RHS of the tree, I will consider refactoring, rather than add another property to MassiveStateObject. Why? Because when you come back to look at MassiveStateObject in two years the implicit coupling between the two components above will not be clear.
Each widget can have a store, each section can have a store, each view can have a store, etc. You decicde what works for you.
(I use mobx in a large and complex trading application)
But we also have multiple smaller stores which are not connected to the root store. Think: multiple not-connected trees where each node is a store.
I must agree that both approaches have pros and cons, e.g. passing down data down a long path of stores is annoying.
Out of curiosity, how do your 'not connected' stores know when, say, a user logs out?
Also, I suppose you have to write code, bespoke to each object, to be able to reset these objects too?
But take the case of an app using local state, this logic if indeed separable from UI, could be coded as functions, rather than within imperative object 'stores'. And these functions would obviously be just as testable.
If it’s read-only, you can just declare it as a constant. No need to add a state management library.
When you have some functional data (the lisk of tasks in a todo app) then putting that in the React context and injecting it in the UI blocks that need it is the simplest way as long the injection part is well separated (using something like unstated.next).
There is some grey area between the two but with this methodology I have never felt the need to use redux or mobx again.
The last time I had to use mobx I was bitten by the rough edges of the decorators.
MobX is not a replacement for local state. It's a useful mechanism for sharing state between parts of an app that need to share state - in the example of a left panel, if you want to be able to control that from outside of the component then you either need to be able to modify the state (eg MobX), or you need a callback that refers to the local panel state that's callable from outside (eg a Redux action), or you need a method that's passed around the app to where it's needed (eg a React context). If you don't need to control it from outside then you don't need any of these things and useState is fine.
Where MobX is especially useful is in things that aren't simply an observable value that need to be passed around - if you need to transform the value for other components to use it then MobX's @computed and @action are both _incredibly_ helpful.
Although I was thinking if you wanted to be a religious zealot about not coding any of your own global state you could use the browser session API.