Search documentation

Search all pages, headings and text, in the current package or topic or in all documentation.

Getting started

Receive active-state in your repository, declare it, and build a first model with a store, a derived value and a React component.

@workspace/active-state 1.0.1Developmentunreleased, in development in Foundationbaseline ce081dc3

  1. Receive the package#

    active-state depends on no other workspace package, so it travels alone. Ask the maintainer of Foundation for a bundle (on the development channel while it is unreleased), then follow Receiving packages:

    Terminal
    node tools/rps/rps-apply.mjs plan --bundle <bundle-folder> --developmentnode tools/rps/rps-apply.mjs apply --bundle <bundle-folder> --expect-plan <digest> --development

    The bundle is composed in Foundation with pnpm rps:transfer bundle --products active-state --channel development --out <folder>.

  2. Declare, install and verify#

    JSON
    {  "dependencies": {    "@workspace/active-state": "workspace:*",    "react": "^19.0.0"  }}
    Terminal
    pnpm installnode tools/rps/rps-apply.mjs verify-install --from <app-folder>

    verify-install must list @workspace/active-state as current. In a Next.js app, add the package to transpilePackages; Vite compiles it as it is.

  3. Build a model#

    A model is a few objects at module scope (or inside a Scope you own):

    TypeScript
    import {Derived, shallow, Store} from '@workspace/active-state';
    export const lines = new Store<{product: string; price: number}[]>([], {  equal: shallow,});export const total = new Derived({  inputs: [lines],  compute: (list) => list.reduce((sum, line) => sum + line.price, 0),});
    lines.update((list) => [...list, {product: 'Pen', price: 1.25}]);total.state; // 1.25
  4. Read it in React#

    Import from /react in a component file; it repeats the core, so one import is enough:

    TSX
    'use client';
    import {useStore} from '@workspace/active-state/react';import {total} from './model';
    export function Total() {  return <output>{useStore(total)}</output>;}

    The same model, running in your browser:

    A store, derived values and useStoreClient

    • Notebook: 4.50

    1 item, total 4.50

Next#

Stores and derived values explains when a write is published and how equality decides it.

This page as Markdown