Skip to content
@React-Statemesh

React Statemesh

StateMesh is a production-grade state management library for React that treats every state change as a transaction. Built with TypeScript, zero runtime.

StateMesh

Transaction-first state orchestration for React

One store. One type system. Zero runtime dependencies.

npm version downloads bundle size TypeScript tests zero dependencies React 18+


The Problem

React apps scatter state across too many libraries. Redux for global state. React Query for server state. React Router for navigation. Formik or React Hook Form for forms. Undo libraries. Persistence layers. Cross-tab sync. Each one has its own API, its own types, its own DevTools, and its own way of doing things.

StateMesh replaces all of that with one store, one set of types, one DevTools panel.

What You Get

State Management

  • External store with path-based subscriptions
  • Named actions with draft mutation
  • Computed state with dependency tracking
  • Batch operations for grouped updates
  • Undo/redo with configurable history
  • State time travel with ring buffer

Async Transactions

  • Full lifecycle: validate, optimistic update, effect, commit, rollback
  • Retry with exponential backoff and jitter
  • Timeout and cancellation via AbortSignal
  • Concurrency policies: takeLatest, block, queue
  • Status tracking and logging

Server State

  • Resource cache with request deduplication
  • Stale-while-revalidate and polling
  • Invalidation tags and refetch triggers
  • Pagination and infinite scroll
  • Mutations with optimistic rollback
  • Offline mutation queueing

Routing

  • Built-in router where routing IS state management
  • Nested routes with lazy loading
  • Route loaders, guards, and middleware
  • Navigation rollback on loader failure
  • Keep-alive pools with LRU eviction
  • Predictive prefetch and shared element transitions

Forms

  • Async field-level validation
  • Schema adapters (Zod, Yup, Standard Schema)
  • Server error mapping
  • Dirty tracking and autosave
  • Field arrays and multi-step wizards
  • Checkbox, radio, select, file helpers

Platform

  • Persistence (localStorage, sessionStorage, IndexedDB)
  • Cross-tab sync (BroadcastChannel, localStorage fallback)
  • URL state with custom param names
  • DevTools with timeline, profiler, and diagnostics
  • Testing utilities with mocks and async helpers
  • Full SSR and Next.js App Router support

Quick Start

npm install react-statemesh
import { createMesh, StateMeshProvider, useMeshState } from "react-statemesh";

const mesh = createMesh({
  state: { count: 0 }
});

function Counter() {
  const [count, setCount] = useMeshState<number>("count");
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}

export function App() {
  return (
    <StateMeshProvider mesh={mesh}>
      <Counter />
    </StateMeshProvider>
  );
}

State reads use useSyncExternalStore. Updating count does not rerender components that don't read it.

How It Works

Every state change in StateMesh is a transaction. This is the core idea. Whether it's a button click, an API call, a form submission, or a route transition, it goes through the same lifecycle:

Action/Mutation/Transaction
  └─ before()        → validate preconditions
  └─ optimistic()    → update UI immediately
  └─ effect()        → call API, write to disk, etc.
  └─ commit()        → apply the real result
  └─ rollback()      → undo the optimistic update on failure
  └─ retry           → exponential backoff with jitter

This means you get optimistic UI, error recovery, and rollback for free on every async operation. Not just API calls — form submissions, route transitions, and custom workflows too.

Architecture

┌─────────────────────────────────────────────────────┐
│                    React Components                  │
│  useMeshState · useMeshResource · useMeshForm · ...  │
└──────────────────────┬──────────────────────────────┘
                       │ useSyncExternalStore
┌──────────────────────┴──────────────────────────────┐
│                     StateMesh Store                  │
│  state · actions · computed · transactions · forms   │
│  resources · mutations · url state · undo/redo       │
├─────────────────────────────────────────────────────┤
│  Middleware · Guards · Pipelines · Event System      │
├─────────────────────────────────────────────────────┤
│  Persistence · Tab Sync · Router · DevTools          │
└─────────────────────────────────────────────────────┘

Subscriptions are path-scoped and equality-checked. Updating cart.items does not rerender components reading theme. Computed values cache until their dependencies change. Resources deduplicate in-flight requests by key.

Why StateMesh

StateMesh Redux + RTK Query Zustand + React Query Jotai
Libraries needed 1 3-4 2-3 2-3
Transactions Built-in Manual Manual No
Optimistic UI Automatic Manual Plugin No
Rollback Automatic Manual Plugin No
Router Built-in Separate Separate Separate
Forms Built-in Separate Separate Separate
Persistence Built-in Plugin Plugin Plugin
Undo/Redo Built-in Plugin No No
Time Travel Built-in Plugin No No
Cross-tab Sync Built-in No No No
Runtime deps 0 4+ 2+ 1+
TypeScript First-class First-class First-class First-class

Documentation

Packages

Package Description Size
react-statemesh Core store, hooks, actions, transactions, forms, URL state, resources 36 KB
react-statemesh/router Built-in router with nested routes, loaders, guards, middleware 36 KB
react-statemesh/devtools DevTools dock with timeline, profiler, and diagnostics 48 KB
react-statemesh/persist Persistence adapters (localStorage, sessionStorage, IndexedDB) 4 KB
react-statemesh/sync Cross-tab sync (BroadcastChannel, localStorage fallback) 4 KB
react-statemesh/resources API client with auth refresh, retry, and upload support 4 KB
react-statemesh/testing Test helpers, mock utilities, and async assertions 8 KB

Community

License

MIT — free for personal and commercial use.

Pinned Loading

  1. statemesh-docs statemesh-docs Public

    A docs site for React Statemesh

    Vue 1

  2. react-statemesh react-statemesh Public

    StateMesh Transaction-first state for React

    TypeScript 1

Repositories

Showing 3 of 3 repositories

People

This organization has no public members. You must be a member to see who’s a part of this organization.

Top languages

Loading…

Most used topics

Loading…