mobx-state-tree

Full-featured reactive state management without the boilerplate

README

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.

  1. ``` js
  2. import { types } from "mobx-state-tree"

  3. // Define a couple models
  4. const Author = types.model({
  5.     id: types.identifier,
  6.     firstName: types.string,
  7.     lastName: types.string
  8. })
  9. const Tweet = types.model({
  10.     id: types.identifier,
  11.     author: types.reference(Author), // stores just the `id` reference!
  12.     body: types.string,
  13.     timestamp: types.number
  14. })

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

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

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

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

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

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

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.