MobX-state-tree

MobX-state-tree

  • Documentation
  • TypeDocs
  • Sponsor
  • GitHub

›Basic Concepts

Introduction

  • Welcome to MobX-State-Tree!
  • Installation
  • Getting Started Tutorial
  • Examples
  • Overview & Philosophy

Basic Concepts

  • Types, models, trees & state
  • Actions
  • Derived values
  • React and MST
  • Snapshots
  • Identifiers and references
  • Asynchronous actions

Advanced Concepts

  • Patches
  • Listening to changes
  • Dependency Injection
  • Middleware
  • Reconciliation
  • Volatile state

API Overview

  • Types overview
  • API overview
  • Lifecycle hooks overview

Tips

  • Talks & Blogs
  • Frequently Asked Questions
  • TypeScript and MST
  • Circular dependencies
  • Simulating inheritance
  • Using snapshots as values
  • Miscellaneous Tips

Compare

  • React Context vs. MobX-State-Tree

Recipes

  • Auto-Generated Property Setter Actions
  • Pre-built Form Types with MST Form Type
  • Manage Asynchronous Data with mst-query
Edit

Snapshots

egghead.io lesson 3: Test mobx-state-tree Models by Recording Snapshots or Patches
Hosted on egghead.io

egghead.io lesson 9: Store Store in Local Storage
Hosted on egghead.io

egghead.io lesson 16: Automatically Send Changes to the Server by Using onSnapshot
Hosted on egghead.io

Snapshots are the immutable serialization, in plain objects, of a tree at a specific point in time. Snapshots can be inspected through getSnapshot(node, applyPostProcess). Snapshots don't contain any type information and are stripped from all actions, etc., so they are perfectly suitable for transportation. Requesting a snapshot is cheap as MST always maintains a snapshot of each node in the background and uses structural sharing.

coffeeTodo.setTitle("Tea instead plz")

console.dir(getSnapshot(coffeeTodo))
// prints `{ title: "Tea instead plz" }`

Some interesting properties of snapshots:

  • Snapshots are immutable
  • Snapshots can be transported
  • Snapshots can be used to update models or restore them to a particular state
  • Snapshots are automatically converted to models when needed. So, the two following statements are equivalent: store.todos.push(Todo.create({ title: "test" })) and store.todos.push({ title: "test" }).

Useful methods:

  • getSnapshot(model, applyPostProcess): returns a snapshot representing the current state of the model
  • onSnapshot(model, callback): creates a listener that fires whenever a new snapshot is available (but only one per MobX transaction).
  • applySnapshot(model, snapshot): updates the state of the model and all its descendants to the state represented by the snapshot

mobx-state-tree also supports customizing snapshots when they are generated or when they are applied with types.snapshotProcessor.

← React and MSTIdentifiers and references →
MobX-state-tree
Docs
Getting StartedBasic conceptsAdvanced conceptsAPI Reference
Community
GitHub DiscussionsStack Overflow
More
BlogGitHubStar
Facebook Open Source
Copyright © 2025 Michel Weststrate