[]previous and next page

Module-level state

useStore needs no owner, so it works at module scope.

For agentsUpdated

useStore needs no owner, so it works at module scope. A module that exports its state gives every component the same store.

src/cart.tsts
import { useStore } from "@shadow-js/core";

export interface CartLine {
  sku: string;
  qty: number;
}

export const [cart, setCart] = useStore<{ lines: CartLine[] }>({ lines: [] });

export function addLine(sku: string): void {
  setCart((draft) => {
    const found = draft.lines.find((line) => line.sku === sku);
    if (found === undefined) draft.lines.push({ sku, qty: 1 });
    else found.qty += 1;
  });
}