Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does mobx-react-lite's "useLocalStore" hook do, and why is it (only sometimes) needed?

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

like image 644
arhnee Avatar asked Dec 06 '19 08:12

arhnee


People also ask

What is the use of MobX in react?

MobX will make sure that all changes to the application state caused by your actions are automatically processed by all derivations and reactions.

What hooks does MobX?

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.

Can you use hooks with MobX?

Hooks to the rescue. React Hooks are most likely the easiest way to consume MobX store.

What is MobX in react native?

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.


2 Answers

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

like image 80
arhnee Avatar answered Sep 21 '22 12:09

arhnee


useLocalStore has been deprecated in favor of useLocalObservable, see here.

like image 45
M K Avatar answered Sep 18 '22 12:09

M K