github statelyai/xstate @xstate/store@3.17.0

14 hours ago

Minor Changes

  • #5479 3b8c68e Thanks @davidkpiano! - Add strategy: 'event' option to the persist extension. Instead of persisting context snapshots, this persists the event log and replays events on rehydration to reconstruct state. When maxEvents is set, a snapshot checkpoint is automatically saved so that replay starts from the checkpoint rather than initial context, preserving correctness.

    Also adds isHydrated(store) helper to check hydration status.

    const store = createStore({
      context: { count: 0 },
      on: { inc: (ctx) => ({ count: ctx.count + 1 }) }
    }).with(
      persist({
        name: 'my-store',
        strategy: 'event',
        maxEvents: 100
      })
    );
  • #5474 e299d40 Thanks @davidkpiano! - Add reset store extension for resetting store context to its initial state via .with(reset()).

    import { createStore } from '@xstate/store';
    import { reset } from '@xstate/store/reset';
    
    const store = createStore({
      context: { count: 0, user: null },
      on: {
        inc: (ctx) => ({ ...ctx, count: ctx.count + 1 }),
        login: (ctx, e: { user: string }) => ({ ...ctx, user: e.user })
      }
    }).with(reset());
    
    store.trigger.inc();
    store.trigger.reset(); // resets to { count: 0, user: null }

    Supports custom reset logic via to for partial resets:

    .with(reset({
      to: (initial, current) => ({ ...initial, user: current.user })
    }))
  • #5472 f7c2beb Thanks @davidkpiano! - Add persist store extension for persisting store context to storage (localStorage, sessionStorage, async adapters, etc.) via .with(persist({ name: 'my-store' })).

    import { createStore } from '@xstate/store';
    import {
      persist,
      rehydrateStore,
      clearStorage,
      flushStorage,
      createJSONStorage
    } from '@xstate/store/persist';
    
    const store = createStore({
      context: { count: 0 },
      on: { inc: (ctx) => ({ count: ctx.count + 1 }) }
    }).with(persist({ name: 'my-store' }));
    // Default storage is localStorage

    Features:

    • Sync and async storage adapters (localStorage, AsyncStorage, etc.)
    • pick — persist only selected fields
    • version + migrate — schema versioning and migration
    • merge — custom merge strategy on rehydration
    • throttle — batched/throttled writes
    • serialize / deserialize — custom serialization
    • filter — skip persisting for specific events
    • skipHydration + rehydrateStore() — manual/async rehydration
    • clearStorage() — remove persisted data
    • flushStorage() — force immediate write of pending throttled data
    • createJSONStorage() — SSR-safe storage adapter factory
    • onDone / onError callbacks

Don't miss a new xstate release

NewReleases is sending notifications on new releases.