github statelyai/xstate @xstate/store-react@2.0.0

Major Changes

  • #5512 063416d Thanks @davidkpiano! - Modernize Store v4 package entrypoints.

    Use framework-specific packages such as @xstate/store-react and @xstate/store-solid instead of @xstate/store/react or @xstate/store/solid. The Store packages now publish ESM package entrypoints.

  • #5512 063416d Thanks @davidkpiano! - Add createStoreLogic(...) for reusable store definitions, and support creating stores from logic in framework hooks.

    const counterLogic = createStoreLogic({
      context: (input: { initialCount: number }) => ({
        count: input.initialCount
      }),
      on: {
        inc: (context) => ({ count: context.count + 1 })
      }
    });
    
    const store = useStore(counterLogic, { initialCount: 0 });

    If a store logic requires input, the input argument is also required:

    useStore(counterLogic, { initialCount: 0 });

    Framework hooks also preserve schema-derived context, event, and emitted event types when creating stores from config objects.

Minor Changes

  • #5512 063416d Thanks @davidkpiano! - Add reusable atom configs and framework atom-state helpers.

    createAtomConfig(...) creates an inert atom definition that can be instantiated with its createAtom(...) method or React/Preact/Vue/Solid's useAtomState(...). These helpers return the current framework-native value and live atom instance, and also work with existing atom instances.

    const countConfig = createAtomConfig((input: { initialCount: number }) => {
      return input.initialCount;
    });
    
    function Counter() {
      const [count, countAtom] = useAtomState(countConfig, { initialCount: 0 });
    
      return (
        <button onClick={() => countAtom.set((count) => count + 1)}>
          {count}
        </button>
      );
    }

Patch Changes

Don't miss a new xstate release

NewReleases is sending notifications on new releases.