AgentSkillsCN

json-render-react

为 json-render 提供 React 渲染器,可将 JSON 规范转化为 React 组件。在使用 @json-render/react 时,可用于从 JSON 构建 React UI、创建组件目录,或渲染由 AI 生成的规范。

SKILL.md
--- frontmatter
name: json-render-react
description: React renderer for json-render that turns JSON specs into React components. Use when working with @json-render/react, building React UIs from JSON, creating component catalogs, or rendering AI-generated specs.

@json-render/react

React renderer that converts JSON specs into React component trees.

Quick Start

typescript
import { Renderer } from "@json-render/react";
import { catalog } from "./catalog";

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

Creating a Catalog

typescript
import { defineCatalog, defineComponents } from "@json-render/react";
import { schema } from "@json-render/react"; // Uses element tree schema
import { z } from "zod";

// Define component implementations
const components = defineComponents(catalog, {
  Button: ({ props }) => (
    <button className={props.variant}>{props.label}</button>
  ),
  Card: ({ props, children }) => (
    <div className="card">
      <h2>{props.title}</h2>
      {children}
    </div>
  ),
});

// Create catalog with props schemas
export const catalog = defineCatalog(schema, {
  components: {
    Button: {
      props: z.object({
        label: z.string(),
        variant: z.enum(["primary", "secondary"]).nullable(),
      }),
      description: "Clickable button",
    },
    Card: {
      props: z.object({ title: z.string() }),
      description: "Card container with title",
    },
  },
});

Spec Structure (Element Tree)

The React schema uses an element tree format:

json
{
  "root": {
    "type": "Card",
    "props": { "title": "Hello" },
    "children": [
      { "type": "Button", "props": { "label": "Click me" } }
    ]
  }
}

Contexts

ContextPurpose
DataContextProvide data for binding ({{path.to.value}})
ActionsContextProvide action handlers
ValidationContextForm validation state
VisibilityContextConditional rendering

Key Exports

ExportPurpose
RendererRender spec to React components
schemaElement tree schema
defineComponentsType-safe component registry
useDataAccess data context
useActionsAccess actions context