mobx
Simple, scalable state management.
Last updated 5 years ago by mweststrate .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install mobx 
SYNC missed versions from official npm registry.
logo

MobX

Simple, scalable state management

CircleCI Coverage Status Join the chat at https://gitter.im/mobxjs/mobx Discuss MobX on Hashnode OpenCollective OpenCollective code style: prettier

MobX is proudly sponsored by Mendix, Coinbase, Facebook Open Source and many individual sponsors

Mendix Coinbase Facebook Open Source

Installation

  • Installation: npm install mobx --save. React bindings: npm install mobx-react --save. To enable ESNext decorators (optional), see below.
  • CDN:

Translations

Getting started

Introduction

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

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

which includes the UI, data serialization, server communication, etc.

MobX unidirectional flow

React and MobX together are a powerful combination. React renders the application state by providing mechanisms to translate it into a tree of renderable components. MobX provides the mechanism to store and update the application state that React then uses.

Both React and MobX provide optimal and unique solutions to common problems in application development. React provides mechanisms to optimally render UI by using a virtual DOM that reduces the number of costly DOM mutations. MobX provides mechanisms to optimally synchronize application state with your React components by using a reactive virtual dependency state graph that is only updated when strictly needed and is never stale.

Core concepts

MobX has only a few core concepts. The following snippets can be tried online using codesandbox example.

Observable state

Egghead.io lesson 1: observable & observer

MobX adds observable capabilities to existing data structures like objects, arrays and class instances. This can simply be done by annotating your class properties with the @observable decorator (ES.Next).

import { observable } from "mobx"

class Todo {
    id = Math.random();
    @observable title = "";
    @observable finished = false;
}

Using observable is like turning a property of an object into a spreadsheet cell. But unlike spreadsheets, these values can be not only primitive values, but also references, objects and arrays.

If your environment doesn't support decorator syntax, don't worry. You can read here about how to set them up. Or you can skip them altoghether, as MobX can be used fine without decorator syntax, by leveraging the decorate utility. Many MobX users do prefer the decorator syntax though, as it is slightly more concise.

import { decorate, observable } from "mobx"

class Todo {
    id = Math.random();
    title = "";
    finished = false;
}
decorate(Todo, {
    title: observable,
    finished: observable
})

Computed values

Egghead.io lesson 3: computed values

With MobX you can define values that will be derived automatically when relevant data is modified. By using the @computed decorator or by using getter / setter functions when using (extend)Observable (Of course, you can use decorate here again as alternative to the @ syntax).

class TodoList {
    @observable todos = [];
    @computed get unfinishedTodoCount() {
        return this.todos.filter(todo => !todo.finished).length;
    }
}

MobX will ensure that unfinishedTodoCount is updated automatically when a todo is added or when one of the finished properties is modified. Computations like these resemble formulas in spreadsheet programs like MS Excel. They update automatically and only when required.

Reactions

Egghead.io lesson 9: custom reactions

Reactions are similar to a computed value, but instead of producing a new value, a reaction produces a side effect for things like printing to the console, making network requests, incrementally updating the React component tree to patch the DOM, etc. In short, reactions bridge reactive and imperative programming.

React components

Egghead.io lesson 1: observable & observer

If you are using React, you can turn your (stateless function) components into reactive components by simply adding the observer function / decorator from the mobx-react package onto them.

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {observer} from 'mobx-react';

@observer
class TodoListView extends Component {
    render() {
        return <div>
            <ul>
                {this.props.todoList.todos.map(todo =>
                    <TodoView todo={todo} key={todo.id} />
                )}
            </ul>
            Tasks left: {this.props.todoList.unfinishedTodoCount}
        </div>
    }
}

const TodoView = observer(({todo}) =>
    <li>
        <input
            type="checkbox"
            checked={todo.finished}
            onClick={() => todo.finished = !todo.finished}
        />{todo.title}
    </li>
)

const store = new TodoList();
ReactDOM.render(<TodoListView todoList={store} />, document.getElementById('mount'));

observer turns React (function) components into derivations of the data they render. When using MobX there are no smart or dumb components. All components render smartly but are defined in a dumb manner. MobX will simply make sure the components are always re-rendered whenever needed, but also no more than that. So the onClick handler in the above example will force the proper TodoView to render, and it will cause the TodoListView to render if the number of unfinished tasks has changed. However, if you would remove the Tasks left line (or put it into a separate component), the TodoListView will no longer re-render when ticking a box. You can verify this yourself by changing the JSFiddle.

Custom reactions

Custom reactions can simply be created using the autorun, reaction or when functions to fit your specific situations.

For example the following autorun prints a log message each time the amount of unfinishedTodoCount changes:

autorun(() => {
    console.log("Tasks left: " + todos.unfinishedTodoCount)
})

What will MobX react to?

Why does a new message get printed each time the unfinishedTodoCount is changed? The answer is this rule of thumb:

MobX reacts to any existing observable property that is read during the execution of a tracked function.

For an in-depth explanation about how MobX determines to which observables needs to be reacted, check understanding what MobX reacts to.

Actions

Egghead.io lesson 5: actions

Unlike many flux frameworks, MobX is unopinionated about how user events should be handled.

  • This can be done in a Flux like manner.
  • Or by processing events using RxJS.
  • Or by simply handling events in the most straightforward way possible, as demonstrated in the above onClick handler.

In the end it all boils down to: Somehow the state should be updated.

After updating the state MobX will take care of the rest in an efficient, glitch-free manner. So simple statements, like below, are enough to automatically update the user interface.

There is no technical need for firing events, calling a dispatcher or what more. A React component in the end is nothing more than a fancy representation of your state. A derivation that will be managed by MobX.

store.todos.push(
    new Todo("Get Coffee"),
    new Todo("Write simpler code")
);
store.todos[0].finished = true;

Nonetheless, MobX has an optional built-in concept of actions. Read this section as well if you want to know more about writing asynchronous actions. It's easy! Use them to your advantage; they will help you to structure your code better and make wise decisions about when and where state should be modified.

MobX: Simple and scalable

MobX is one of the least obtrusive libraries you can use for state management. That makes the MobX approach not just simple, but very scalable as well:

Using classes and real references

With MobX you don't need to normalize your data. This makes the library very suitable for very complex domain models (At Mendix for example ~500 different domain classes in a single application).

Referential integrity is guaranteed

Since data doesn't need to be normalized, and MobX automatically tracks the relations between state and derivations, you get referential integrity for free. Rendering something that is accessed through three levels of indirection?

No problem, MobX will track them and re-render whenever one of the references changes. As a result staleness bugs are a thing of the past. As a programmer you might forget that changing some data might influence a seemingly unrelated component in a corner case. MobX won't forget.

Simpler actions are easier to maintain

As demonstrated above, modifying state when using MobX is very straightforward. You simply write down your intentions. MobX will take care of the rest.

Fine grained observability is efficient

MobX builds a graph of all the derivations in your application to find the least number of re-computations that is needed to prevent staleness. "Derive everything" might sound expensive, MobX builds a virtual derivation graph to minimize the number of recomputations needed to keep derivations in sync with the state.

In fact, when testing MobX at Mendix we found out that using this library to track the relations in our code is often a lot more efficient than pushing changes through our application by using handwritten events or "smart" selector based container components.

The simple reason is that MobX will establish far more fine grained 'listeners' on your data than you would do as a programmer.

Secondly MobX sees the causality between derivations so it can order them in such a way that no derivation has to run twice or introduces a glitch.

How that works? See this in-depth explanation of MobX.

Easy interoperability

MobX works with plain javascript structures. Due to its unobtrusiveness it works with most javascript libraries out of the box, without needing MobX specific library flavors.

So you can simply keep using your existing router, data fetching, and utility libraries like react-router, director, superagent, lodash etc.

For the same reason you can use it out of the box both server and client side, in isomorphic applications and with react-native.

The result of this is that you often need to learn less new concepts when using MobX in comparison to other state management solutions.


Credits

MobX is inspired by reactive programming principles as found in spreadsheets. It is inspired by MVVM frameworks like in MeteorJS tracker, knockout and Vue.js. But MobX brings Transparent Functional Reactive Programming to the next level and provides a stand alone implementation. It implements TFRP in a glitch-free, synchronous, predictable and efficient manner.

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

And finally kudos for all the people that believed in, tried, validated and even sponsored MobX.

Further resources and documentation

What others are saying...

Guise, #mobx isn't pubsub, or your grandpa's observer pattern. Nay, it is a carefully orchestrated observable dimensional portal fueled by the power cosmic. It doesn't do change detection, it's actually a level 20 psionic with soul knife, slashing your viewmodel into submission.

After using #mobx for lone projects for a few weeks, it feels awesome to introduce it to the team. Time: 1/2, Fun: 2X

Working with #mobx is basically a continuous loop of me going “this is way too simple, it definitely won’t work” only to be proven wrong

Try react-mobx with es6 and you will love it so much that you will hug someone.

I have built big apps with MobX already and comparing to the one before that which was using Redux, it is simpler to read and much easier to reason about.

The #mobx is the way I always want things to be! It's really surprising simple and fast! Totally awesome! Don't miss it!

Contributing

  • Feel free to send small pull requests. Please discuss new features or big changes in a GitHub issue first.
  • Use npm test to run the basic test suite, npm run coverage for the test suite with coverage and npm run perf for the performance tests.

Flow support

MobX ships with flow typings. Flow will automatically include them when you import mobx modules. Although you do not need to import the types explicitly, you can still do it like this: import type { ... } from 'mobx'.

To use the flow typings shipped with MobX:

  • In .flowconfig, you cannot ignore node_modules.
  • In .flowconfig, you cannot import it explicitly in the [libs] section.
  • You do not need to install library definition using flow-typed.

Donating

Was MobX key in making your project a success? Join our open collective or use the donate button!

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer]

One time donations through paypal are welcome as well and are recorded in the sponsors list.

Donate

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]

Current Tags

  • 6.11.0-pre                                ...           beta (a year ago)
  • 6.13.3                                ...           latest (2 months ago)
  • 4.15.7                                ...           mobx4 (4 years ago)

249 Versions

  • 6.13.3                                ...           2 months ago
  • 6.13.2                                ...           2 months ago
  • 6.13.1                                ...           4 months ago
  • 6.13.0                                ...           4 months ago
  • 6.12.5                                ...           5 months ago
  • 6.12.4                                ...           5 months ago
  • 6.12.3                                ...           8 months ago
  • 6.12.1                                ...           8 months ago
  • 6.12.0                                ...           a year ago
  • 6.11.0                                ...           a year ago
  • 6.11.0-pre                                ...           a year ago
  • 6.10.2                                ...           a year ago
  • 6.10.1                                ...           a year ago
  • 6.10.0                                ...           a year ago
  • 6.9.1                                ...           a year ago
  • 6.9.0                                ...           2 years ago
  • 6.8.0                                ...           2 years ago
  • 6.7.0                                ...           2 years ago
  • 6.6.2                                ...           2 years ago
  • 6.6.1                                ...           2 years ago
  • 6.6.0                                ...           2 years ago
  • 6.5.0                                ...           3 years ago
  • 6.4.2                                ...           3 years ago
  • 6.4.1                                ...           3 years ago
  • 6.4.0                                ...           3 years ago
  • 6.3.13                                ...           3 years ago
  • 6.3.12                                ...           3 years ago
  • 6.3.11                                ...           3 years ago
  • 6.3.10                                ...           3 years ago
  • 6.3.9                                ...           3 years ago
  • 6.3.8                                ...           3 years ago
  • 6.3.7                                ...           3 years ago
  • 6.3.6                                ...           3 years ago
  • 6.3.5                                ...           3 years ago
  • 6.3.4                                ...           3 years ago
  • 6.3.3                                ...           3 years ago
  • 6.3.2                                ...           3 years ago
  • 6.3.1                                ...           3 years ago
  • 6.3.0                                ...           4 years ago
  • 6.2.0                                ...           4 years ago
  • 6.1.8                                ...           4 years ago
  • 6.1.7                                ...           4 years ago
  • 6.1.6                                ...           4 years ago
  • 6.1.5                                ...           4 years ago
  • 6.1.4                                ...           4 years ago
  • 6.1.3                                ...           4 years ago
  • 6.1.2                                ...           4 years ago
  • 6.1.1                                ...           4 years ago
  • 6.1.0                                ...           4 years ago
  • 6.0.5                                ...           4 years ago
  • 6.0.4                                ...           4 years ago
  • 6.0.3                                ...           4 years ago
  • 6.0.2                                ...           4 years ago
  • 6.0.1                                ...           4 years ago
  • 6.0.0                                ...           4 years ago
  • 6.0.0-rc.10                                ...           4 years ago
  • 6.0.0-rc.9                                ...           4 years ago
  • 5.15.7                                ...           4 years ago
  • 4.15.7                                ...           4 years ago
  • 6.0.0-rc.8                                ...           4 years ago
  • 6.0.0-rc.7                                ...           4 years ago
  • 6.0.0-rc.6                                ...           4 years ago
  • 6.0.0-rc.5                                ...           4 years ago
  • 6.0.0-rc.4                                ...           4 years ago
  • 6.0.0-rc.3                                ...           4 years ago
  • 6.0.0-rc.2                                ...           4 years ago
  • 4.15.6                                ...           4 years ago
  • 5.15.6                                ...           4 years ago
  • 5.15.5                                ...           4 years ago
  • 4.15.5                                ...           4 years ago
  • 6.0.0-rc.1                                ...           4 years ago
  • 6.0.0-rc.0                                ...           4 years ago
  • 4.15.4                                ...           5 years ago
  • 5.15.4                                ...           5 years ago
  • 5.15.3                                ...           5 years ago
  • 4.15.3                                ...           5 years ago
  • 4.15.2                                ...           5 years ago
  • 5.15.2                                ...           5 years ago
  • 4.15.1                                ...           5 years ago
  • 5.15.1                                ...           5 years ago
  • 5.15.0                                ...           5 years ago
  • 4.15.0                                ...           5 years ago
  • 5.14.2                                ...           5 years ago
  • 4.14.1                                ...           5 years ago
  • 5.14.1                                ...           5 years ago
  • 4.14.0                                ...           5 years ago
  • 5.14.0                                ...           5 years ago
  • 4.13.1                                ...           5 years ago
  • 5.13.1                                ...           5 years ago
  • 5.13.0                                ...           5 years ago
  • 4.13.0                                ...           5 years ago
  • 4.12.0                                ...           5 years ago
  • 5.11.0                                ...           5 years ago
  • 4.11.0                                ...           5 years ago
  • 5.10.1                                ...           5 years ago
  • 5.10.0                                ...           5 years ago
  • 4.10.0                                ...           5 years ago
  • 5.9.4-special                                ...           5 years ago
  • 5.9.4                                ...           6 years ago
  • 4.9.4                                ...           6 years ago
  • 4.9.3                                ...           6 years ago
  • 4.9.2                                ...           6 years ago
  • 4.9.1                                ...           6 years ago
  • 5.9.0                                ...           6 years ago
  • 4.9.0                                ...           6 years ago
  • 4.8.0                                ...           6 years ago
  • 5.8.0                                ...           6 years ago
  • 5.7.0                                ...           6 years ago
  • 4.7.0                                ...           6 years ago
  • 4.6.0                                ...           6 years ago
  • 5.6.0                                ...           6 years ago
  • 4.5.2                                ...           6 years ago
  • 5.5.2                                ...           6 years ago
  • 4.5.1                                ...           6 years ago
  • 5.5.1                                ...           6 years ago
  • 5.5.0                                ...           6 years ago
  • 4.5.0                                ...           6 years ago
  • 4.4.2                                ...           6 years ago
  • 5.1.2                                ...           6 years ago
  • 4.4.1                                ...           6 years ago
  • 5.1.1                                ...           6 years ago
  • 5.1.0                                ...           6 years ago
  • 4.4.0                                ...           6 years ago
  • 5.0.5                                ...           6 years ago
  • 5.0.4                                ...           6 years ago
  • 4.3.2                                ...           6 years ago
  • 5.0.3                                ...           6 years ago
  • 5.0.2                                ...           6 years ago
  • 5.0.0                                ...           6 years ago
  • 5.0.0-beta.7                                ...           6 years ago
  • 4.3.1                                ...           6 years ago
  • 5.0.0-beta.6                                ...           6 years ago
  • 5.0.0-beta.5                                ...           6 years ago
  • 5.0.0-beta.4                                ...           6 years ago
  • 5.0.0-beta.3                                ...           6 years ago
  • 5.0.0-beta.2                                ...           6 years ago
  • 5.0.0-beta.1                                ...           6 years ago
  • 4.3.0                                ...           6 years ago
  • 4.2.1                                ...           7 years ago
  • 4.2.0                                ...           7 years ago
  • 4.1.1                                ...           7 years ago
  • 4.1.0                                ...           7 years ago
  • 4.0.2-fix-rn                                ...           7 years ago
  • 4.0.2                                ...           7 years ago
  • 4.0.1                                ...           7 years ago
  • 4.0.0                                ...           7 years ago
  • 3.6.2                                ...           7 years ago
  • 4.0.0-beta.4                                ...           7 years ago
  • 4.0.0-beta.3                                ...           7 years ago
  • 4.0.0-beta.2                                ...           7 years ago
  • 4.0.0-beta.1                                ...           7 years ago
  • 4.0.0-alpha.2                                ...           7 years ago
  • 3.6.1                                ...           7 years ago
  • 4.0.0-alpha.1                                ...           7 years ago
  • 3.6.0                                ...           7 years ago
  • 3.5.1                                ...           7 years ago
  • 3.4.1                                ...           7 years ago
  • 3.4.0                                ...           7 years ago
  • 3.3.3                                ...           7 years ago
  • 3.3.2                                ...           7 years ago
  • 3.3.1                                ...           7 years ago
  • 3.3.0                                ...           7 years ago
  • 3.2.2                                ...           7 years ago
  • 3.2.1                                ...           7 years ago
  • 3.2.0                                ...           7 years ago
  • 3.1.17                                ...           7 years ago
  • 3.1.16                                ...           7 years ago
  • 3.1.15                                ...           7 years ago
  • 3.1.14                                ...           7 years ago
  • 3.1.13-fixcra                                ...           7 years ago
  • 3.1.11                                ...           7 years ago
  • 3.1.11-rollup                                ...           7 years ago
  • 3.1.10                                ...           7 years ago
  • 3.1.9                                ...           8 years ago
  • 3.1.8-fix874                                ...           8 years ago
  • 3.1.8                                ...           8 years ago
  • 3.1.7                                ...           8 years ago
  • 3.1.6-fix887                                ...           8 years ago
  • 3.1.5                                ...           8 years ago
  • 3.1.4                                ...           8 years ago
  • 3.1.3                                ...           8 years ago
  • 3.1.2                                ...           8 years ago
  • 3.1.0                                ...           8 years ago
  • 3.0.2-fix798-2                                ...           8 years ago
  • 3.0.2-fix798                                ...           8 years ago
  • 3.0.1-fix734-2                                ...           8 years ago
  • 3.0.2                                ...           8 years ago
  • 3.0.1-fix734                                ...           8 years ago
  • 3.0.1                                ...           8 years ago
  • 3.0.0-fix-751                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 3.0.0-rc.2                                ...           8 years ago
  • 3.0.0-rc.1                                ...           8 years ago
  • 2.7.0                                ...           8 years ago
  • 2.6.5                                ...           8 years ago
  • 2.6.4                                ...           8 years ago
  • 2.6.3                                ...           8 years ago
  • 2.6.2                                ...           8 years ago
  • 2.7.0-beta                                ...           8 years ago
  • 2.6.1                                ...           8 years ago
  • 2.6.0-fix621                                ...           8 years ago
  • 2.6.0                                ...           8 years ago
  • 2.5.2                                ...           8 years ago
  • 2.5.1                                ...           8 years ago
  • 2.5.0                                ...           8 years ago
  • 2.5.0-rc1                                ...           8 years ago
  • 2.4.4                                ...           8 years ago
  • 2.4.3-fix505                                ...           8 years ago
  • 2.5.0-alpha1                                ...           8 years ago
  • 2.4.3                                ...           8 years ago
  • 2.4.2                                ...           8 years ago
  • 2.4.1                                ...           8 years ago
  • 2.4.0-fix441                                ...           8 years ago
  • 2.4.0-fix427                                ...           8 years ago
  • 2.4.0                                ...           8 years ago
  • 2.4.0-beta1                                ...           8 years ago
  • 2.4.0-alpha1                                ...           8 years ago
  • 2.3.7                                ...           8 years ago
  • 2.3.6-albert                                ...           8 years ago
  • 2.3.6                                ...           8 years ago
  • 2.3.4-iosfix3                                ...           8 years ago
  • 2.3.5                                ...           8 years ago
  • 2.3.4-iosfix2                                ...           8 years ago
  • 2.3.4-iosfix                                ...           8 years ago
  • 2.3.4                                ...           8 years ago
  • 2.3.3                                ...           8 years ago
  • 2.3.2                                ...           8 years ago
  • 2.3.1                                ...           8 years ago
  • 2.3.0                                ...           8 years ago
  • 2.2.2                                ...           8 years ago
  • 2.2.1                                ...           8 years ago
  • 2.2.0                                ...           8 years ago
  • 2.2.0-beta.1                                ...           8 years ago
  • 2.1.7                                ...           8 years ago
  • 2.1.6                                ...           9 years ago
  • 2.1.5                                ...           9 years ago
  • 2.1.4                                ...           9 years ago
  • 2.1.3                                ...           9 years ago
  • 2.1.2                                ...           9 years ago
  • 2.1.1                                ...           9 years ago
  • 2.1.0                                ...           9 years ago
  • 2.0.6                                ...           9 years ago
  • 2.0.5                                ...           9 years ago
  • 2.0.4                                ...           9 years ago
  • 2.0.3                                ...           9 years ago
  • 2.0.2                                ...           9 years ago
  • 2.0.1                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 0.0.0                                ...           9 years ago
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (34)

© 2010 - cnpmjs.org x YWFE | Home | YWFE