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?
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.
It is not mandatory to use react context with Mobx but it is recommended now officially on the mobx-react website.
Mobx autorun is synchronous normally, but becomes non-synchronous when an action is running #1911.
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.
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)
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.
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