MobX

Simple, scalable state management.

README

logo

MobX


_Simple, scalable state management._
npm version OpenCollective OpenCollective Discuss on Github Coverage Status View changelog


Documentation


Documentation can be found at mobx.js.org.


Sponsors


MobX is made possible by the generosity of the sponsors below, and many other individual backers. Sponsoring directly impacts the longevity of this project.

**🥇 Gold sponsors (\$3000+ total contribution):**
MendixGuildedParallaxFrontend MastersOne BeyondAuction FrontierCodeFirstModulzCoinbaseCurologyFacebook Open SourceCanva

**🥈 Silver sponsors (\$100+ per month):**
Casino SitesUPPER

**🥉 Bronze sponsors (\$500+ total contributions):**
BugsnagSpace307mantro GmbHAlgoliatalentplotDAZNBlokt


Introduction


_Anything that can be derived from the application state, should be. Automatically._

MobX is a battle-tested library that makes state management simple and scalable by transparently applying functional reactive programming.
The philosophy behind MobX is simple:

😙

Straightforward

Write minimalistic, boilerplate-free code that captures your intent.

            Trying to update a record field? Simply use a normal JavaScript assignment —
            the reactivity system will detect all your changes and propagate them out to where they are being used.
            No special tools are required when updating data in an asynchronous process.

🚅

Effortless optimal rendering

                All changes to and uses of your data are tracked at runtime, building a dependency tree that captures all relations between state and output.
                This guarantees that computations that depend on your state, like React components, run only when strictly needed.
                There is no need to manually optimize components with error-prone and sub-optimal techniques like memoization and selectors.

🤹🏻‍♂️

Architectural freedom

                MobX is unopinionated and allows you to manage your application state outside of any UI framework.
                This makes your code decoupled, portable, and above all, easily testable.



A quick example


So what does code that uses MobX look like?

  1. ``` js
  2. import React from "react"
  3. import ReactDOM from "react-dom"
  4. import { makeAutoObservable } from "mobx"
  5. import { observer } from "mobx-react"

  6. // Model the application state.
  7. class Timer {
  8.     secondsPassed = 0

  9.     constructor() {
  10.         makeAutoObservable(this)
  11.     }

  12.     increase() {
  13.         this.secondsPassed += 1
  14.     }

  15.     reset() {
  16.         this.secondsPassed = 0
  17.     }
  18. }

  19. const myTimer = new Timer()

  20. // Build a "user interface" that uses the observable state.
  21. const TimerView = observer(({ timer }) => (
  22.     <button onClick={() => timer.reset()}>Seconds passed: {timer.secondsPassed}</button>
  23. ))

  24. ReactDOM.render(<TimerView timer={myTimer} />, document.body)

  25. // Update the 'Seconds passed: X' text every second.
  26. setInterval(() => {
  27.     myTimer.increase()
  28. }, 1000)
  29. ```

The observer wrapper around the TimerView React component will automatically detect that rendering
depends on the timer.secondsPassed observable, even though this relationship is not explicitly defined. The reactivity system will take care of re-rendering the component when _precisely that_ field is updated in the future.

Every event (onClick / setInterval) invokes an _action_ (myTimer.increase / myTimer.reset) that updates _observable state_ (myTimer.secondsPassed).
Changes in the observable state are propagated precisely to all _computations_ and _side effects_ (TimerView) that depend on the changes being made.

MobX unidirectional flow

This conceptual picture can be applied to the above example, or any other application using MobX.

Getting started


To learn about the core concepts of MobX using a larger example, check out The gist of MobX page, or take the 10 minute interactive introduction to MobX and React.
The philosophy and benefits of the mental model provided by MobX are also described in great detail in the blog posts UI as an afterthought and How to decouple state and UI (a.k.a. you don’t need componentWillMount).

Further resources


-   The MobX cheat sheet (£5) is both useful and sponsors the project
-   The MobX awesome list – a long list of MobX resources and example projects

The MobX book



The MobX Quick Start Guide ($24.99) by Pavan Podila and Michel Weststrate is available as an ebook, paperback, and on the O'Reilly platform (see preview).

Videos


-   Introduction to MobX & React in 2020 by Leigh Halliday, _17 min_.
-   ReactNext 2016: Real World MobX by Michel Weststrate, _40 min_, slides.
-   React Amsterdam 2016: State Management Is Easy by Michel Weststrate, _20 min_, slides.
-   {🚀} React Live 2019: Reinventing MobX by Max Gallo, _27 min_.

Credits


MobX is inspired by reactive programming principles, which are for example used in spreadsheets. It is inspired by model–view–viewmodel frameworks like MeteorJS's Tracker, Knockout and Vue.js, but MobX brings _transparent functional reactive programming_ (TFRP, a concept which is further explained in the MobX book) to the next level and provides a standalone implementation. It implements TFRP in a glitch-free, synchronous, predictable and efficient manner.

A ton of credit goes to Mendix, for providing the flexibility and support to maintain MobX and the chance to prove the philosophy of MobX in a real, complex, performance critical applications.