UNPKG

4.61 kBMarkdownView Raw
1<img src="website/static/img/mobx-state-tree-logo-gradient.png" alt="logo" height="120" align="right" />
2
3# mobx-state-tree
4
5[![npm version](https://badge.fury.io/js/mobx-state-tree.svg)](https://badge.fury.io/js/mobx-state-tree)
6[![CircleCI](https://circleci.com/gh/mobxjs/mobx-state-tree.svg?style=svg)](https://circleci.com/gh/mobxjs/mobx-state-tree)
7[![Coverage Status](https://coveralls.io/repos/github/mobxjs/mobx-state-tree/badge.svg?branch=master)](https://coveralls.io/github/mobxjs/mobx-state-tree?branch=master)
8[![Have a question? Ask on GitHub Discussions!](https://img.shields.io/badge/Have%20a%20question%3F-Ask%20on%20GitHub%20Discussions!-blue)](https://github.com/mobxjs/mobx-state-tree/discussions)
9
10## What is mobx-state-tree?
11
12Technically speaking, mobx-state-tree (also known as MST) is a state container system built on [MobX](https://github.com/mobxjs/mobx), a functional reactive state library.
13
14This 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!
15
16MobX is [one of the most popular Redux alternatives](https://2019.stateofjs.com/data-layer/mobx/) 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](https://github.com/mobxjs/mobx/tree/main/packages/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.
17
18_Note: you don't need to know how to use MobX in order to use MST._
19
20# Getting started
21
22See the [Getting started](https://mobx-state-tree.js.org/intro/getting-started) tutorial or follow the free [egghead.io course](https://egghead.io/courses/manage-application-state-with-mobx-state-tree).
23
24👉 Official docs can be found at [http://mobx-state-tree.js.org/](http://mobx-state-tree.js.org/)
25
26## Quick Code Example
27
28There'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.
29
30```js
31import { types } from "mobx-state-tree"
32
33// Define a couple models
34const Author = types.model({
35 id: types.identifier,
36 firstName: types.string,
37 lastName: types.string
38})
39const Tweet = types.model({
40 id: types.identifier,
41 author: types.reference(Author), // stores just the `id` reference!
42 body: types.string,
43 timestamp: types.number
44})
45
46// Define a store just like a model
47const RootStore = types.model({
48 authors: types.array(Author),
49 tweets: types.array(Tweet)
50})
51
52// Instantiate a couple model instances
53const jamon = Author.create({
54 id: "jamon",
55 firstName: "Jamon",
56 lastName: "Holmgren"
57})
58
59const tweet = Tweet.create({
60 id: "1",
61 author: jamon.id, // just the ID needed here
62 body: "Hello world!",
63 timestamp: Date.now()
64})
65
66// Now instantiate the store!
67const rootStore = RootStore.create({
68 authors: [jamon],
69 tweets: [tweet]
70})
71
72// Ready to use in a React component, if that's your target.
73import { observer } from "mobx-react-lite"
74const MyComponent = observer((props) => {
75 return <div>Hello, {rootStore.authors[0].firstName}!</div>
76})
77
78// Note: since this component is "observed", any changes to rootStore.authors[0].firstName
79// will result in a re-render! If you're not using React, you can also "listen" to changes
80// using `onSnapshot`: https://mobx-state-tree.js.org/concepts/snapshots
81```
82
83## Thanks!
84
85- [Michel Weststrate](https://twitter.com/mweststrate) for creating MobX, MobX-State-Tree, and MobX-React.
86- [Infinite Red](https://infinite.red) for supporting ongoing maintenance on MST.
87- [Mendix](https://mendix.com) for sponsoring and providing the opportunity to work on exploratory projects like MST.
88- [Dan Abramov](https://twitter.com/dan_abramov)'s work on [Redux](http://redux.js.org) has strongly influenced the idea of snapshots and transactional actions in MST.
89- [Giulio Canti](https://twitter.com/GiulioCanti)'s work on [tcomb](http://github.com/gcanti/tcomb) and type systems in general has strongly influenced the type system of MST.
90- All the early adopters encouraging to pursue this whole idea and proving it is something feasible.