• Stars
    star
    6,867
  • Rank 5,451 (Top 0.2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated 19 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Full-featured reactive state management without the boilerplate

logo

mobx-state-tree

npm version CircleCI Codecov Have a question? Ask on GitHub Discussions!

What is mobx-state-tree?

Technically speaking, mobx-state-tree (also known as MST) is a state container system built on MobX, a functional reactive state library.

This may not mean much to you, and thatโ€™s okay. Iโ€™ll explain it like this: MobX is a state management "engine", and MobX-State-Tree gives it structure and common tools you need for your app. MST is valuable in a large team but also useful in smaller applications when you expect your code to scale rapidly. And if we compare it to Redux, MST offers better performance and much less boilerplate code than Redux!

MobX is one of the most popular Redux alternatives and is used (along with MobX-State-Tree) by companies worldwide. MST plays very well with TypeScript, React, and React Native, especially when paired with mobx-react-lite. It supports multiple stores, async actions and side effects, enables extremely targeted re-renders for React apps, and much more -- all in a package with zero dependencies other than MobX itself.

Note: you don't need to know how to use MobX in order to use MST.

Getting started

See the Getting started tutorial or follow the free egghead.io course.

๐Ÿ‘‰ Official docs can be found at http://mobx-state-tree.js.org/

Quick Code Example

There's nothing quite like looking at some code to get a feel for a library. Check out this small example of an author and list of tweets by that author.

import { types } from "mobx-state-tree"

// Define a couple models
const Author = types.model({
    id: types.identifier,
    firstName: types.string,
    lastName: types.string
})
const Tweet = types.model({
    id: types.identifier,
    author: types.reference(Author), // stores just the `id` reference!
    body: types.string,
    timestamp: types.number
})

// Define a store just like a model
const RootStore = types.model({
    authors: types.array(Author),
    tweets: types.array(Tweet)
})

// Instantiate a couple model instances
const jamon = Author.create({
    id: "jamon",
    firstName: "Jamon",
    lastName: "Holmgren"
})

const tweet = Tweet.create({
    id: "1",
    author: jamon.id, // just the ID needed here
    body: "Hello world!",
    timestamp: Date.now()
})

// Now instantiate the store!
const rootStore = RootStore.create({
    authors: [jamon],
    tweets: [tweet]
})

// Ready to use in a React component, if that's your target.
import { observer } from "mobx-react-lite"
const MyComponent = observer((props) => {
    return <div>Hello, {rootStore.authors[0].firstName}!</div>
})

// Note: since this component is "observed", any changes to rootStore.authors[0].firstName
// will result in a re-render! If you're not using React, you can also "listen" to changes
// using `onSnapshot`: https://mobx-state-tree.js.org/concepts/snapshots

Thanks!

  • Michel Weststrate for creating MobX, MobX-State-Tree, and MobX-React.
  • Infinite Red for supporting ongoing maintenance on MST.
  • Mendix for sponsoring and providing the opportunity to work on exploratory projects like MST.
  • Dan Abramov's work on Redux has strongly influenced the idea of snapshots and transactional actions in MST.
  • Giulio Canti's work on tcomb and type systems in general has strongly influenced the type system of MST.
  • All the early adopters encouraging to pursue this whole idea and proving it is something feasible.

More Repositories

1

mobx

Simple, scalable state management.
TypeScript
27,224
star
2

mobx-react

React bindings for MobX
TypeScript
4,864
star
3

mobx.dart

MobX for the Dart language. Hassle-free, reactive state-management for your Dart and Flutter apps.
Dart
2,365
star
4

awesome-mobx

A collection of awesome things regarding MobX.
2,176
star
5

mobx-react-lite

Lightweight React bindings for MobX based on React 16.8 and Hooks
TypeScript
2,132
star
6

mobx-react-devtools

[DEPRECATED] Tools to perform runtime analyses of React applications powered by MobX and React
JavaScript
1,228
star
7

mobx-utils

Utility functions and common patterns for MobX
TypeScript
1,168
star
8

mobx-react-boilerplate

Small project to quickly start with React, MobX, JSX, ES6, Babel
JavaScript
895
star
9

serializr

Serialize and deserialize complex object graphs to and from JSON and Javascript classes
TypeScript
760
star
10

mst-gql

Bindings for mobx-state-tree and GraphQL
JavaScript
676
star
11

mobx-react-todomvc

TodoMVC reference implementation on top of react-mobx-boilerplate
JavaScript
501
star
12

mobx-angular

The MobX connector for Angular.
TypeScript
479
star
13

mobx-devtools

Mobx Devtools (React, Chrome Extension) - Looking for maintainers! https://github.com/mobxjs/mobx-devtools/issues/55
JavaScript
479
star
14

mobx-vue

๐Ÿ‰ Vue bindings for MobX
TypeScript
475
star
15

mobx-examples

A collection of simple mobx examples
HTML
285
star
16

create-react-app-mobx

DEPRECATED. Use https://github.com/arackaf/customize-cra
JavaScript
146
star
17

mobx-preact

MobX bindings for Preact
JavaScript
125
star
18

babel-plugin-mobx-deep-action

Reduces `action` and `runInAction` boilerplates
JavaScript
109
star
19

mobx-reactive2015-demo

Runnable source code of the #GoReactive #mobservable talk
JavaScript
98
star
20

mobx-contacts-list

React Amsterdam 2016 Demo Project
JavaScript
77
star
21

mobx-vue-lite

Lightweight Vue 3 bindings for MobX based on Composition API.
TypeScript
64
star
22

mobx-angularjs

MobX connector to AngularJS
TypeScript
51
star
23

mobx-react-docz

DEPRECATED Documentation site for MobX in React
TypeScript
42
star
24

zh.mobx.js.org

Mobxไธญๆ–‡ๆ–‡ๆกฃ
HTML
39
star
25

react-particles-experiment

An experiment to see if you can make a particle generator in redux+react+d3.
JavaScript
28
star
26

ko.mobx.js.org

Mobx korean document
HTML
11
star
27

mobxjs.github.io

Redir to mobx documentation
HTML
1
star
28

.github

1
star
29

mst-codemod-to-0.10

A codemod to migrate to MobX-State-Tree 0.10 from previous versions
TypeScript
1
star