jotai-state-tree
v1.19.2
Published
MobX-State-Tree API compatible library powered by Jotai
Downloads
10,882
Maintainers
Readme
jotai-state-tree
A MobX-State-Tree (MST) compatible state management library powered by Jotai.
jotai-state-tree combines the transactional, tree-structured state model of MobX-State-Tree with the lightweight, zero-leak, high-performance atomic updates of Jotai. It is designed to be an API-compatible, drop-in replacement for MobX-State-Tree, featuring perfect TypeScript type safety out of the box.
Features
- MST-Compatible API - Familiar
types.model,types.array,types.mapand more - Powered by Jotai - Leverages Jotai's atomic state model for high performance
- No Memory Leaks - Relies on Jotai's garbage collection model (no dangling subscriptions)
- Snapshots & Patches - Full support for
getSnapshot,applySnapshot,onPatch - Tree Navigation -
getRoot,getParent,getPath,resolvePath - References - Type-safe references with
types.referenceandtypes.safeReference - React Integration - Fine-grained reactive observers and hooks
- Zero Production Overhead - Write protection checks are bypassed completely in production
- Mixins & Composition - Reusable, type-safe mixins with
types.mixinand.apply() - Advanced Utilities - Built-in undo managers, time travel, and action recorders
Installation
npm install jotai-state-tree jotaiReact Native Compatibility
jotai-state-tree is fully compatible with React Native projects.
Prerequisites & JS Engine
- React Native Version:
>= 0.70is required. - JavaScript Engine: The library relies on native ES2021
WeakRefandFinalizationRegistryfeatures for memory management. If you use the Hermes engine (default since React Native 0.70), it must be version0.12.0or newer.
Using the Router in React Native
When running in React Native (or any non-browser environment), the built-in state router automatically disables DOM/browser integration and behaves as a fully-featured in-memory router. It maintains a navigation history stack internally, enabling you to use:
push(path)/replace(path)go(delta)/goBack()/goForward()RouteViewto reactively render screen components based on the active path
This allows you to manage native navigation state trees with full time-travel, middleware, and action recording support!
Quick Start
import { types, getSnapshot } from 'jotai-state-tree';
const Todo = types
.model('Todo', {
id: types.identifier,
title: types.string,
done: types.optional(types.boolean, false),
})
.actions((self) => ({
toggle() {
self.done = !self.done;
},
}));
const store = Todo.create({ id: '1', title: 'Learn jotai-state-tree' });
store.toggle();
console.log(getSnapshot(store)); // { id: '1', title: 'Learn jotai-state-tree', done: true }Documentation Guides
Explore our detailed, exhaustive guides to master jotai-state-tree:
- Getting Started - Installation, core architecture, and a complete quickstart application.
- Models & State - Defining models, views, actions, protection rules, volatile states, lifecycle hooks, and snapshot processing.
- Types & Composition - Exhaustive list of primitives, identifiers, collections, union types, recursive structures (
types.late), references, composition, and mixins. - Tree Utilities - Serialization (snapshots & patches), hierarchy navigation, traversal (
walk,find), and relative path resolution. - React Integration - Observables HOCs, typed context Providers, hooks (
useSnapshot,useWatchPath), and update batching. - Advanced Features - Undo/Redo managers, Time Travel, Action recorders, dynamic plugins/registry, and middleware pipelines.
- Migration from MobX-State-Tree - Step-by-step replacement guide, performance comparisons, and key differences.
- Examples & Templates - 2 production-ready project starters (SPA and SSR) and 8 specialized example templates, features breakdown, and project scaffolding instructions.
License
MIT
