Within the mobx-react documentation there are variations in how stores are created. For example, on the React Context page:
In the first code sample, the store is instantiated with useLocalStore:
const store = useLocalStore(createStore)
In the second code sample, the stores are initiated by directly "newing" the stores":
counterStore: new CounterStore(),
themeStore: new ThemeStore(),
By inference, the first is a "local" store (and thus needs useLocalStore), and the second is a "global" store and thus doesn't. However, it is not clear why this is, and what the subsequent differnce in behaviour is.
Why is useLocalStore
not needed in second example, and what difference does this make to the behavior of the stores and mobx within React?
Thanks for any input
MobX will make sure that all changes to the application state caused by your actions are automatically processed by all derivations and reactions.
The hook is available only with mobx-react-lite library or mobx-react@6 . Low level implementation used internally by observer HOC and Observer component.
Hooks to the rescue. React Hooks are most likely the easiest way to consume MobX store.
MobX is one of the most popular state management libraries used by applications sized from small to large. With the introduction of the new React Context API, MobX can now be very easily integrated in React Native Navigation projects.
OK, I found the answer. useLocalStore
turns a javascript literal into a store with observable properties. This is not needed if a store is created from a class object with observable attributes.
Thanks to @freddyc for the answer
useLocalStore
has been deprecated in favor of useLocalObservable
, see here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With