MCPcopy
hub / github.com/mobxjs/mobx-state-tree

github.com/mobxjs/mobx-state-tree @v7.3.1 sqlite

repository ↗ · DeepWiki ↗ · release v7.3.1 ↗
1,075 symbols 3,202 edges 112 files 24 documented · 2%
README

logo

mobx-state-tree

npm version CircleCI 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" // alternatively: import { t } 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 

Hello, {rootStore.authors[0].firstName}!


})

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

Extension points exported contracts — how you extend this code

IMapType (Interface)
(no doc) [2 implementers]
src/types/complex-types/map.ts
IPatchRecorder (Interface)
(no doc)
src/core/mst-operations.ts
IActionTrackingMiddlewareHooks (Interface)
(no doc)
src/middlewares/create-action-tracking-middleware.ts
IRootModel (Interface)
(no doc)
__tests__/core/lazy.test.ts
IArrayType (Interface)
(no doc) [2 implementers]
src/types/complex-types/array.ts
IPatches (Interface)
(no doc)
src/core/mst-operations.ts
IActionTrackingMiddleware2Call (Interface)
(no doc)
src/middlewares/createActionTrackingMiddleware2.ts
IModelProps (Interface)
(no doc)
__tests__/core/2230.test.ts

Core symbols most depended-on inside this repo

create
called by 785
src/core/type/type.ts
getSnapshot
called by 243
src/core/type/type.ts
push
called by 186
src/types/complex-types/array.ts
unprotect
called by 168
src/core/mst-operations.ts
actions
called by 154
src/types/complex-types/model.ts
is
called by 143
src/core/type/type.ts
get
called by 110
src/types/complex-types/map.ts
instantiate
called by 84
src/core/type/type.ts

Shape

Function 602
Method 332
Class 67
Interface 67
Enum 7

Languages

TypeScript100%

Modules by API surface

__tests__/core/type-system.test.ts60 symbols
src/core/node/object-node.ts58 symbols
src/core/mst-operations.ts56 symbols
src/types/complex-types/model.ts55 symbols
src/types/complex-types/map.ts55 symbols
src/utils.ts48 symbols
src/types/utility-types/reference.ts46 symbols
src/types/complex-types/array.ts37 symbols
src/core/type/type.ts37 symbols
src/types/utility-types/identifier.ts27 symbols
__tests__/core/object.test.ts25 symbols
src/types/utility-types/snapshotProcessor.ts24 symbols

Dependencies from manifests, versioned

@size-limit/preset-big-lib5.0.3 · 1×
@types/bun1.0.6 · 1×
@types/node12.0.2 · 1×
concat1.0.3 · 1×
coveralls3.1.0 · 1×
cpr3.0.1 · 1×
docusaurus1.14.0 · 1×
husky7.0.0 · 1×
lint-staged11.1.2 · 1×
mobx6.13.1 · 1×
prettier3.4.1 · 1×
rollup2.18.1 · 1×

For agents

$ claude mcp add mobx-state-tree \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact