Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does mobx achieve its magic

Tags:

reactjs

mobx

I am curious as to how mobx works under the hood.

If I have a component that is both an observer and contains observables:

@observer
export default class Form extends Component {
  @observable submitted = false;

  @action.bound
  submit() {
  }
}

How does mobx cause a re-render without using setState or does it use setState?

like image 891
dagda1 Avatar asked May 03 '17 21:05

dagda1


People also ask

How does MobX-react work?

MobX reacts to any existing observable property that is read during the execution of a tracked function. "reading" is dereferencing an object's property, which can be done through "dotting into" it (eg. user.name ) or using the bracket notation (eg. user['name'] , todos[3] ) or destructuring (eg.

Does MobX use context?

It is not mandatory to use react context with Mobx but it is recommended now officially on the mobx-react website.

Is MobX synchronous?

Mobx autorun is synchronous normally, but becomes non-synchronous when an action is running #1911.

Does MobX work with functional components?

In this tutorial, we will learn how to use MobX with React Functional Components. MobX being a very useful state management library reduces the code that needs to be written for global state management. Mobx is easy to use and quick to learn.


2 Answers

See: https://medium.com/@mweststrate/becoming-fully-reactive-an-in-depth-explanation-of-mobservable-55995262a254. @observer is basically a shorthand for autorun(() => this.render()) (it is actually a bit more complex, but that is what it conceptualy boils down to)

like image 78
mweststrate Avatar answered Sep 26 '22 15:09

mweststrate


To understand how mobx works under the hood, check this repl out.

I learned about it from one of the videos of @mweststrate and an academic paper from someone. Both of which I cannot find anymore.

like image 42
Asur Avatar answered Sep 24 '22 15:09

Asur