I have seen React in action for the first time in relatively complex legacy application. There were some attempts to develop new functions using React and, of course, Redux. Because everyone is using that combination. It is the clear, cool way. Hm…
- You never know whether you have plain JS object or Immutable.js object under your hands.
- If you are not using Immutable.js then you still should know whether object you have can be mutated or not.
- In every file you have to manually import libraries like Immutable or Lodash.
I had those issues plus:
- It was real pain to describe complex change you would like to perform on immutable state within store. Like add 6th item into array, remove 3 different keys from map, etc.
- For doing even simple things you have to modify code in multiple files. One often do a hack instead of nice solution.
- Problems with using multiple stores. I still had feeling that I should not store state of form in global store. Local data should be local, right?
- Same store is used during navigation without page reload. It is hard to clean everything properly.
- React-Redux library is doing incredible magic I was never able fully understand.
- Using Promises just made thing worse.
- I still had feeling that if I have to reorganize store structure I will got mad.
Then, out of blue sky, I have found MobX. And I realized that I found great React’s partner.
I can have multiple stores – one that holds application state (selected language, global formatting settings), second that holds form state and handles communication with server, third for caching currency conversions, etc. This stores are available through React context – therefore their presence is checked by React using contextTypes definition. You can freely move components in hierarchy as long as you assure that proper stores are in context.
I still have certain level of immutability because MobX checks that no change is done during processing previous change.
Adding functionality is deadly simple. You have two classes – component and store.
Because store is single purpose and is object of class, it is quite easy to determine what you can do with it. If there is method, try to call it. Compare that with searching for proper Redux action.