npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@xmachines/json-render-react

v0.23.1-xm.1

Published

React renderer for @xmachines/json-render-core. JSON becomes React components.

Readme

@xmachines/json-render-react

React renderer for json-render. Turn JSON specs into React components with data binding, visibility, and actions.

Installation

npm install @xmachines/json-render-react @xmachines/json-render-core zod

Quick Start

1. Create a Catalog

import { defineCatalog } from "@xmachines/json-render-core";
import { schema } from "@xmachines/json-render-react/schema";
import { z } from "zod";

export const catalog = defineCatalog(schema, {
  components: {
    Card: {
      props: z.object({
        title: z.string(),
        description: z.string().nullable(),
      }),
      description: "A card container",
    },
    Button: {
      props: z.object({
        label: z.string(),
        action: z.string(),
      }),
      description: "A clickable button",
    },
    Input: {
      props: z.object({
        value: z.union([z.string(), z.record(z.unknown())]).nullable(),
        label: z.string(),
        placeholder: z.string().nullable(),
      }),
      description: "Text input field with optional value binding",
    },
  },
  actions: {
    submit: { description: "Submit the form" },
    cancel: { description: "Cancel and close" },
  },
});

2. Define Component Implementations

defineRegistry conditionally requires the actions field only when the catalog declares actions. Catalogs with actions: {} can omit it entirely.

import { defineRegistry, useBoundProp } from "@xmachines/json-render-react";
import { catalog } from "./catalog";

export const { registry } = defineRegistry(catalog, {
  components: {
    Card: ({ props, children }) => (
      <div className="card">
        <h3>{props.title}</h3>
        {props.description && <p>{props.description}</p>}
        {children}
      </div>
    ),
    Button: ({ props, emit }) => (
      <button onClick={() => emit("press")}>{props.label}</button>
    ),
    Input: ({ props, bindings }) => {
      const [value, setValue] = useBoundProp(props.value, bindings?.value);
      return (
        <label>
          {props.label}
          <input
            placeholder={props.placeholder ?? ""}
            value={value ?? ""}
            onChange={(e) => setValue(e.target.value)}
          />
        </label>
      );
    },
  },
});

3. Render Specs

import { Renderer, StateProvider, ActionProvider } from "@xmachines/json-render-react";
import { registry } from "./registry";

function App({ spec }) {
  return (
    <StateProvider initialState={{ form: { name: "" } }}>
      <ActionProvider
        handlers={{
          submit: () => console.log("Submit"),
        }}
      >
        <Renderer spec={spec} registry={registry} />
      </ActionProvider>
    </StateProvider>
  );
}

Spec Format

The React renderer uses a flat element map format:

interface Spec {
  root: string; // Key of the root element
  elements: Record<string, UIElement>; // Flat map of elements by key
  state?: Record<string, unknown>; // Optional initial state
}

interface UIElement {
  type: string; // Component name from catalog
  props: Record<string, unknown>; // Component props
  children?: string[]; // Keys of child elements
  slots?: Record<string, string[]>; // Named slots mapped to child keys
  visible?: VisibilityCondition; // Visibility condition
}

The slots element field is a React renderer feature. Other renderer packages may only use catalog slot declarations for default children.

Example spec:

{
  "root": "card-1",
  "elements": {
    "card-1": {
      "type": "Card",
      "props": { "title": "Welcome" },
      "children": ["input-1", "btn-1"]
    },
    "input-1": {
      "type": "Input",
      "props": {
        "value": { "$bindState": "/form/name" },
        "label": "Name",
        "placeholder": "Enter name"
      }
    },
    "btn-1": {
      "type": "Button",
      "props": { "label": "Submit" },
      "children": []
    }
  }
}

Contexts

StateProvider

Share data across components with JSON Pointer paths:

<StateProvider initialState={{ user: { name: "John" } }}>
  {children}
</StateProvider>;

// In components:
const { state, get, set } = useStateStore();
const name = get("/user/name"); // "John"
set("/user/age", 25);

External Store (Controlled Mode)

For full control over state, pass a StateStore to bypass the internal state and wire json-render to any state management library (Redux, Zustand, XState, etc.):

import { createStateStore, type StateStore } from "@xmachines/json-render-react";

// Option 1: Use the built-in store outside of React
const store = createStateStore({ count: 0 });

<StateProvider store={store}>{children}</StateProvider>;

// Mutate from anywhere — React will re-render automatically:
store.set("/count", 1);

// Option 2: Implement the StateStore interface with your own backend
const zustandStore: StateStore = {
  get: (path) => getByPath(useStore.getState(), path),
  set: (path, value) =>
    useStore.setState((prev) => {
      /* ... */
    }),
  update: (updates) =>
    useStore.setState((prev) => {
      /* ... */
    }),
  getSnapshot: () => useStore.getState(),
  subscribe: (listener) => useStore.subscribe(listener),
};

When store is provided, initialState and onStateChange are ignored. The store is the single source of truth. The same store prop is available on createRenderer, JSONUIProvider, and StateProvider.

ActionProvider

Handle actions from components:

<ActionProvider
  handlers={{
    submit: (params) => handleSubmit(params),
    cancel: () => handleCancel(),
  }}
>
  {children}
</ActionProvider>

VisibilityProvider

Control element visibility based on data:

<VisibilityProvider>
  {children}
</VisibilityProvider>

// Elements can use visibility conditions:
{
  "type": "Alert",
  "props": { "message": "Error!" },
  "visible": { "$state": "/form/hasError" }
}

ValidationProvider

Add field validation:

<ValidationProvider>{children}</ValidationProvider>;

// Use validation hooks:
const { errors, validate } = useFieldValidation("/form/email", {
  checks: [
    { type: "required", message: "Email required" },
    { type: "email", message: "Invalid email" },
  ],
});

Hooks

| Hook | Purpose | | ---------------------------------- | --------------------------------------------------------------- | | useStateStore() | Access state context (state, get, set, update) | | useStateValue(path) | Get single value from state | | useStateBinding(path) | Two-way data binding (returns [value, setValue]) | | useIsVisible(condition) | Check if a visibility condition is met | | useActions() | Access action context | | useAction(binding) | Run one action binding (returns { execute, isLoading }) | | useFieldValidation(path, config) | Field validation state | | useOptionalValidation() | Non-throwing validation context (returns null if no provider) | | useUIStream(options) | Stream specs from an API endpoint |

Visibility Conditions

// Truthiness check
{ "$state": "/user/isAdmin" }

// Auth state (use state path)
{ "$state": "/auth/isSignedIn" }

// Comparisons (flat style)
{ "$state": "/status", "eq": "active" }
{ "$state": "/count", "gt": 10 }

// Negation
{ "$state": "/maintenance", "not": true }

// Multiple conditions (implicit AND)
[
  { "$state": "/feature/enabled" },
  { "$state": "/maintenance", "not": true }
]

// Always / never
true   // always visible
false  // never visible

TypeScript helpers from @xmachines/json-render-core:

import { visibility } from "@xmachines/json-render-core";

visibility.when("/path"); // { $state: "/path" }
visibility.unless("/path"); // { $state: "/path", not: true }
visibility.eq("/path", val); // { $state: "/path", eq: val }
visibility.neq("/path", val); // { $state: "/path", neq: val }
visibility.and(cond1, cond2); // { $and: [cond1, cond2] }
visibility.always; // true
visibility.never; // false

Dynamic Prop Expressions

Any prop value can use data-driven expressions that resolve at render time. The renderer resolves these transparently before passing props to components.

{
  "type": "Badge",
  "props": {
    "label": { "$state": "/user/role" },
    "color": {
      "$cond": { "$state": "/user/role", "eq": "admin" },
      "$then": "red",
      "$else": "gray"
    }
  }
}

For two-way binding, use { "$bindState": "/path" } on the natural value prop (e.g. value, checked, pressed). Inside repeat scopes, use { "$bindItem": "field" } instead.

To render a filtered list (kanban columns, status sections), put repeat and an $item visibility condition on the same container: { "repeat": { "statePath": "/tasks", "key": "id" }, "visible": { "$item": "status", "eq": "todo" }, "children": ["task-card"] } renders one child per matching item. AND-composed $state conjuncts still gate the container itself. Components receive resolved bindings with the state path for each bound prop; use useBoundProp(props.value, bindings?.value) to get [value, setValue].

$template and $computed

{
  "label": { "$template": "Hello, ${/user/name}!" },
  "fullName": {
    "$computed": "fullName",
    "args": {
      "first": { "$state": "/form/firstName" },
      "last": { "$state": "/form/lastName" }
    }
  }
}

Register functions via the functions prop on JSONUIProvider or createRenderer:

<JSONUIProvider
  spec={spec}
  catalog={catalog}
  functions={{ fullName: (args) => `${args.first} ${args.last}` }}
>

See @xmachines/json-render-core for full expression syntax.

State Watchers

Elements can declare a watch field to trigger actions when state values change:

{
  "type": "Select",
  "props": {
    "label": "Country",
    "value": { "$bindState": "/form/country" },
    "options": ["US", "Canada", "UK"]
  },
  "watch": {
    "/form/country": {
      "action": "loadCities",
      "params": { "country": { "$state": "/form/country" } }
    }
  },
  "children": []
}

watch is a top-level field on elements (sibling of type/props/children), not inside props. Watchers only fire on value changes, not on initial render.

Built-in Actions

The setState, pushState, removeState, push, pop, and validateForm actions are built into the React schema and handled automatically by ActionProvider. They are injected into AI prompts without needing to be declared in your catalog's actions. push puts /currentScreen on the /navStack array and sets /currentScreen to its screen param. pop takes the last entry off /navStack and makes it /currentScreen again:

{
  "type": "Button",
  "props": { "label": "Switch Tab" },
  "on": {
    "press": {
      "action": "setState",
      "params": { "statePath": "/activeTab", "value": "settings" }
    }
  },
  "children": []
}

validateForm

Validate all registered form fields at once and write the result to state:

{
  "type": "Button",
  "props": { "label": "Submit" },
  "on": {
    "press": [
      { "action": "validateForm", "params": { "statePath": "/formResult" } },
      { "action": "submitForm" }
    ]
  },
  "children": []
}

Writes { valid: boolean, errors: Record<string, string[]> } to the specified state path (defaults to /formValidation).

Component Props

When using defineRegistry, components receive these props:

interface ComponentContext<P> {
  props: P; // Typed props from the catalog (expressions resolved)
  children?: React.ReactNode; // Rendered children
  slots?: Record<string, React.ReactNode>; // Rendered named slots
  emit: (event: string) => void; // Emit a named event (always defined)
  on: (event: string) => EventHandle; // Get event handle with metadata
  loading?: boolean; // Whether the parent is loading
  bindings?: Record<string, string>; // State paths for $bindState/$bindItem expressions (e.g. bindings.value)
}

interface EventHandle {
  emit: () => void; // Fire the event
  shouldPreventDefault: boolean; // Whether any binding requested preventDefault
  bound: boolean; // Whether any handler is bound
}

Use children for the catalog's "default" slot. Components with additional slots receive them by name:

Layout: ({ children, slots }) => (
  <div>
    <header>{slots?.header}</header>
    <main>{children}</main>
    <footer>{slots?.footer}</footer>
  </div>
),

Use emit("press") for simple event firing. Use on("click") when you need to check metadata like shouldPreventDefault or bound:

Link: ({ props, on }) => {
  const click = on("click");
  return (
    <a
      href={props.href}
      onClick={(e) => {
        if (click.shouldPreventDefault) e.preventDefault();
        click.emit();
      }}
    >
      {props.label}
    </a>
  );
},

Use bindings?.value, bindings?.checked, etc. with useBoundProp() for two-way bound form components.

BaseComponentProps

For building reusable component libraries that are not tied to a specific catalog (e.g. @xmachines/json-render-shadcn), use the catalog-agnostic BaseComponentProps type:

import type { BaseComponentProps } from "@xmachines/json-render-react";

const Card = ({ props, children }: BaseComponentProps<{ title?: string }>) => (
  <div>{props.title}{children}</div>
);

Generate AI Prompts

const systemPrompt = catalog.prompt();
// Returns detailed prompt with component/action descriptions

Full Example

import { defineCatalog } from "@xmachines/json-render-core";
import { schema } from "@xmachines/json-render-react/schema";
import { defineRegistry, Renderer } from "@xmachines/json-render-react";
import { z } from "zod";

const catalog = defineCatalog(schema, {
  components: {
    Greeting: {
      props: z.object({ name: z.string() }),
      description: "Displays a greeting",
    },
  },
  actions: {},
});

const { registry } = defineRegistry(catalog, {
  components: {
    Greeting: ({ props }) => <h1>Hello, {props.name}!</h1>,
  },
});

const spec = {
  root: "greeting-1",
  elements: {
    "greeting-1": {
      type: "Greeting",
      props: { name: "World" },
      children: [],
    },
  },
};

function App() {
  return <Renderer spec={spec} registry={registry} />;
}

Render Errors

Renderer and the component of createRenderer take an onRenderError prop of the type OnRenderError. defineRegistry(catalog, { onRenderError }) puts the handler on the registry, and the prop takes the place of that handler.

The renderer calls onRenderError(error, name, info) one time for each failure in code that it runs. info.kind gives the place of the failure:

| info.kind | Failure | name | Result | | ------------------- | ------------------------------------------------------------------------------------------ | ---------------------- | ------------------------------------------------------------------------------------------------------------------------- | | element | A component throws in its render, in an effect or in a lifecycle method. | the element type | The element shows nothing until a new version of it comes. | | props / visible | A prop expression ($computed, a directive) or the visible condition of a child throws. | the type of the parent | The parent shows nothing. info.key and info.type name the child. | | root | The root component, a prop expression of the root, or its visible condition throws. | the root type | The renderer shows nothing until a new version of the root comes. The error never goes to the error boundary of the host. | | action / watch | A binding of an on chain or of a watch chain throws or rejects. | the action name | The chain stops. The element stays. |

info.key is the key of the element, info.type is its type, and info.action is the action name of a chain. Without a handler, the renderer writes one console.error line, for example [json-render] props error in <Text> "title":, and then the error.

<Renderer
  spec={spec}
  registry={registry}
  onRenderError={(error, name, info) => report(error, info.kind, info.key)}
/>

A handler with two parameters, (error, name), is still a valid handler. If the handler throws, the renderer gives that error to reportError one time, and no other element fails.

The renderer does not report an error of code that it does not run. That code is a listener of a component, a timer, or a promise that the component drops. Such an error goes to the window, as in any React application. The promise of useActions().execute and of useAction().execute goes to the caller.

An element type with no component and no fallback gives one [json-render] warning when the element mounts. A spec.root that names no element gives one warning after the spec loads.

React 19 also writes each error that an error boundary catches to console.error, through the onCaughtError option of the root. So with the default option, the console shows a reported error a second time. The renderer cannot turn this off. Only the host can, with its own onCaughtError. The option applies to each error boundary of the root:

createRoot(container, {
  onCaughtError: (error, errorInfo) => tracker.capture(error, errorInfo),
});

Key Exports

Nested lists can set repeat.statePath to { "$item": "field" } to iterate an array on the enclosing repeat item.

| Export | Purpose | | ------------------ | ---------------------------------------------------------------------------------------------------------------------- | | defineRegistry | Create a type-safe component registry from a catalog | | Renderer | Render a spec using a registry | | schema | Element tree schema (includes built-in actions: setState, pushState, removeState, push, pop, validateForm) | | useStateStore | Access state context | | useStateValue | Get single value from state | | useBoundProp | Two-way binding for $bindState/$bindItem expressions | | useActions | Access actions context | | useAction | Get a single action dispatch function | | useUIStream | Stream specs from an API endpoint | | createStateStore | Create a framework-agnostic in-memory StateStore |

Types

| Export | Purpose | | -------------------- | ----------------------------------------------------------- | | ComponentContext | Typed component render function context (catalog-aware) | | BaseComponentProps | Catalog-agnostic base type for reusable component libraries | | EventHandle | Event handle with emit(), shouldPreventDefault, bound | | ComponentFn | Component render function type | | SetState | State setter type | | StateModel | State model type | | StateStore | Interface for plugging in external state management |