Almost all examples (even the official documentation) use mobx-react-light
in combination with useContext()
hook.
However, React, many articles and blog-posts advice to NOT USE useContext()
for middle/high frequent updates. Isn't state something which can update very frequenty?
Should one use the package in combination with the hook or will performance issues occur?
useContext()
is only used to get your store value (reference) and this value is not updated frequently, usually you only set your stores once and don't touch it after. When you use actions you only change observable values of your store and not the store itself. So basicallyContext
is only used to pass reference to the store down the tree, after that all the work performed by MobX only.Example from MobX docs:
So you pass value of
new Timer()
toTimerContext.Provider
once when you render your app and it will never be changed or updated after that. Even docs says:However, if you don't have SSR or don't test your app, then you don't even need to use Context at all, you can just use global variables/singletons as your stores and it will work perfectly fine.
Example:
Quote from docs:
More about best practices with React: https://mobx.js.org/react-integration.html