MobX-state-tree

MobX-state-tree

  • Documentation
  • TypeDocs
  • Sponsor
  • GitHub

›API Overview

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

API overview

See the TypeDocs for full details and typings.

signature
addDisposer(node, () => void)Add a function to be invoked whenever the target node is about to be destroyed
addMiddleware(node, middleware: (actionDescription, next) => any, includeHooks)Attaches middleware to a node. See middleware. Returns disposer.
applyAction(node, actionDescription)Replays an action on the targeted node
applyPatch(node, jsonPatch)Applies a JSON patch, or array of patches, to a node in the tree
applySnapshot(node, snapshot)Updates a node with the given snapshot
cast(nodeOrSnapshot)Cast a node instance or snapshot to a node instance so it can be used in assignment operations
castToSnapshot(nodeOrSnapshot)Cast a node instance to a snapshot so it can be used inside create operations
castToReferenceSnapshot(node)Cast a node instance to a reference snapshot so it can be used inside create operations
createActionTrackingMiddlewareUtility to make writing middleware that tracks async actions less cumbersome. Consider migrating to createActionTrackingMiddleware2
createActionTrackingMiddleware2Utility to make writing middleware that tracks async actions less cumbersome
clone(node, keepEnvironment?: boolean / newEnvironment)Creates a full clone of the given node. By default preserves the same environment
decorate(handler, function)Attaches middleware to a specific action (or flow)
destroy(node)Kills node, making it unusable. Removes it from any parent in the process
detach(node)Removes node from its current parent, and lets it live on as standalone tree
flow(generator)Creates an asynchronous flow based on a generator function
castFlowReturn(value)Casts a flow return value so it can be correctly inferred as return type. Only needed when using TypeScript and when returning a Promise.
getChildType(node, property?)Returns the declared type of the given property of node. For arrays and maps property can be omitted as they all have the same type
getEnv(node)Returns the environment of node, see dependency injection
getParent(node, depth=1)Returns the intermediate parent of the node, or a higher one if depth > 1
getParentOfType(node, type)Return the first parent that satisfies the provided type
getPath(node)Returns the path of node in the tree
getPathParts(node)Returns the path of node in the tree, unescaped as separate parts
getRelativePath(base, target)Returns the short path, which one could use to walk from node base to node target, assuming they are in the same tree. Up is represented as ../
getRoot(node)Returns the root element of the tree containing node
getIdentifier(node)Returns the identifier of the given element
getNodeId(node)Returns the unique node id (not to be confused with the instance identifier) for a given instance
getSnapshot(node, applyPostProcess)Returns the snapshot of the node. See snapshots
getType(node)Returns the type of node
hasParent(node, depth=1)Returns true if node has a parent at depth
hasParentOfType(node, type)Returns true if the node has a parent that satisfies the provided type
isAlive(node)Returns true if node is alive
isStateTreeNode(value)Returns true if value is a node of a mobx-state-tree
isProtected(value)Returns true if the given node is protected, see actions
isValidReference(() => node / null / undefined, checkIfAlive = true)Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns if the check passes or not.
isRoot(node)Returns true if node has no parents
joinJsonPath(parts)Joins and escapes the given path parts into a JSON path
onAction(node, (actionDescription) => void)A built-in middleware that calls the provided callback with an action description upon each invocation. Returns disposer
onPatch(node, (patch) => void)Attach a JSONPatch listener, that is invoked for each change in the tree. Returns disposer
onSnapshot(node, (snapshot) => void)Attach a snapshot listener, that is invoked for each change in the tree. Returns disposer
process(generator)DEPRECATED – replaced with flow
protect(node)Protects an unprotected tree against modifications from outside actions
recordActions(node)Creates a recorder that listens to all actions in node. Call .stop() on the recorder to stop this, and .replay(target) to replay the recorded actions on another tree
recordPatches(node)Creates a recorder that listens to all patches emitted by the node. Call .stop() on the recorder to stop this, and .replay(target) to replay the recorded patches on another tree
getMembers(node)Returns the model name, properties, actions, views, volatiles of a model node instance
getPropertyMembers(typeOrNode)Returns the model name and properties of a model type for either a model type or a model node
resolve(node, path)Resolves a path (json path) relatively to the given node
resolveIdentifier(type, target, identifier)resolves an identifier of a given type in a model tree
resolvePath(target, path)resolves a JSON path, starting at the specified target
setLivelinessChecking("warn" / "ignore" / "error")Defines what MST should do when running into reads / writes to objects that have died. By default it will print a warning. Use te "error" option to easy debugging to see where the error was thrown and when the offending read / write took place
getLivelinessChecking()Returns the current liveliness checking mode.
splitJsonPath(path)Splits and unescapes the given JSON path into path parts
typecheck(type, value)Typechecks a value against a type. Throws on errors. Use this if you need typechecks even in a production build. NOTE: set process.env.ENABLE_TYPE_CHECK = "true" if you want to enable type checking in any environment
tryResolve(node, path)Like resolve, but just returns null if resolving fails at any point in the path
tryReference(() => node / null / undefined, checkIfAlive = true)Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if it the check passes, else it returns undefined.
unprotect(node)Unprotects node, making it possible to directly modify any value in the subtree, without actions
walk(startNode, (node) => void)Performs a depth-first walk through a tree
escapeJsonPath(path)escape special characters in an identifier, according to http://tools.ietf.org/html/rfc6901
unescapeJsonPath(path)escape special characters in an identifier, according to http://tools.ietf.org/html/rfc6901
isType(value)Returns if a given value represents a type.
isArrayType(value)Returns if a given value represents an array type.
isFrozenType(value)Returns if a given value represents a frozen type.
isIdentifierType(value)Returns if a given value represents an identifier type.
isLateType(value)Returns if a given value represents a late type.
isLiteralType(value)Returns if a given value represents a literal type.
isMapType(value)Returns if a given value represents a map type.
isModelType(value)Returns if a given value represents a model type.
isOptionalType(value)Returns if a given value represents an optional type.
isPrimitiveType(value)Returns if a given value represents a primitive type.
isReferenceType(value)Returns if a given value represents a reference type.
isRefinementType(value)Returns if a given value represents a refinement type.
isUnionType(value)Returns if a given value represents a union type.
getRunningActionContext()Returns the currently executing MST action context, or undefined if none.
isActionContextChildOf(actionContext, parent)Returns if the given action context is a parent of this action context.
isActionContextThisOrChildOf(actionContext, parentOrSame)Returns if the given action context is this or a parent of this action context.

A disposer is a function that cancels the effect for which it was created.

← Types overviewLifecycle hooks overview →
MobX-state-tree
Docs
Getting StartedBasic conceptsAdvanced conceptsAPI Reference
Community
GitHub DiscussionsStack Overflow
More
BlogGitHubStar
Facebook Open Source
Copyright © 2025 Michel Weststrate