mobx README

The project's README from github.com/mobxjs/mobx. As one markdown file: README.md.

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.

🥇🥇 Platinum sponsors ($5000+ total contribution): 🥇🥇


Guilded Canva Parallax

🥇 Gold sponsors ($2500+ total contribution):


One Beyond Frontend Masters Auction Frontier CodeFirst Modulz Coinbase Curology Mendix Facebook Open Source Casino Sites Bugsnag

🥈 Silver sponsors ($500+ total contributions):

mantro GmbH Extremely Heavy Industries Algolia Space307 Blokt UPPER DAZN talentplot EaseUS Route Planner and Route Optimizer Handsontable

Introduction

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

MobX is a signal based, 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?

import React from "react"
import ReactDOM from "react-dom"
import { makeAutoObservable } from "mobx"
import { observer } from "mobx-react-lite"

// Model the application state.
const myTimer = makeAutoObservable({
    secondsPassed: 0,
    increase() {
        this.secondsPassed += 1
    },
    reset() {
        this.secondsPassed = 0
    }
})

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

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

// Update the 'Seconds passed: X' text every second.
setInterval(() => {
    myTimer.increase()
}, 1000)

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 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

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.

mobx (mobxjs/mobx)

Searching Database…

28.2K ·GitHub
这是什么?MobX 是一个用于 JavaScript 应用的状态管理库,借助透明的函数式响应式编程,让所有基于应用状态派生出来的内容自动更新。
这是框架还是库?它是一个库,而不是框架。它没有强制的约定,可以在任何 UI 框架之外管理状态,不过通常与 React 搭配使用。
我该如何把它加入项目?通过 npm 安装,用 'observable' 包裹可观察状态,用 'observer' 包裹响应式组件,使其在依赖变化时自动更新。
使用它一定要用 React 吗?不需要。MobX 本身没有任何依赖,可以独立使用;React 绑定只是常见用法之一。
我需要手动优化重新渲染吗?不需要。MobX 会在运行时自动构建依赖树,因此只有真正依赖已变化状态的组件才会重新渲染,无需手动做记忆化或使用选择器。
为什么它被称为基于信号的库?MobX 在运行时追踪状态并构建依赖图,使变化能够精确、同步地传播到依赖它们的计算和界面部分,类似电子表格的重新计算方式。
MobX 的设计受到了什么启发?MobX 借鉴了电子表格以及 Meteor 的 Tracker、Knockout、Vue.js 等 MVVM 框架中使用的响应式编程原则,并以无中间状态错乱、同步的方式实现透明的函数式响应式编程。
除了 README,我还能去哪里学习 MobX?官方文档位于 mobx.js.org,此外还有一个 10 分钟的交互式 React+MobX 教程、一门 Egghead.io 课程,以及一本付费的《MobX Quick Start Guide》,供深入学习。
What are similar projects?xstate, rxdb, vueuse, formily, pinia, redux, zustand, formik
Creator
mobxjs
Size
22.4 MB
Stars
28.2K
Age
11y
Commits · 90 Days