
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
mobx-state-tree
Advanced tools
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.
See the Getting started tutorial or follow the free egghead.io course.
👉 Official docs can be found at http://mobx-state-tree.js.org/
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 <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
FAQs
Opinionated, transactional, MobX powered state container
The npm package mobx-state-tree receives a total of 71,359 weekly downloads. As such, mobx-state-tree popularity was classified as popular.
We found that mobx-state-tree demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.