json-render-react
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese@json-render/react
@json-render/react
React renderer that converts JSON specs into React component trees.
一款可将JSON规格转换为React组件树的React渲染器。
Quick Start
快速开始
typescript
import { defineRegistry, Renderer } from "@json-render/react";
import { catalog } from "./catalog";
const { registry } = defineRegistry(catalog, {
components: {
Card: ({ props, children }) => <div>{props.title}{children}</div>,
},
});
function App({ spec }) {
return <Renderer spec={spec} registry={registry} />;
}typescript
import { defineRegistry, Renderer } from "@json-render/react";
import { catalog } from "./catalog";
const { registry } = defineRegistry(catalog, {
components: {
Card: ({ props, children }) => <div>{props.title}{children}</div>,
},
});
function App({ spec }) {
return <Renderer spec={spec} registry={registry} />;
}Creating a Catalog
创建组件目录
typescript
import { defineCatalog } from "@json-render/core";
import { schema, defineRegistry } from "@json-render/react";
import { z } from "zod";
// 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",
},
},
});
// Define component implementations with type-safe props
const { registry } = defineRegistry(catalog, {
components: {
Button: ({ props }) => (
<button className={props.variant}>{props.label}</button>
),
Card: ({ props, children }) => (
<div className="card">
<h2>{props.title}</h2>
{children}
</div>
),
},
});typescript
import { defineCatalog } from "@json-render/core";
import { schema, defineRegistry } from "@json-render/react";
import { z } from "zod";
// 结合属性Schema创建组件目录
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",
},
},
});
// 基于组件目录定义类型安全的组件实现
const { registry } = defineRegistry(catalog, {
components: {
Button: ({ props }) => (
<button className={props.variant}>{props.label}</button>
),
Card: ({ props, children }) => (
<div className="card">
<h2>{props.title}</h2>
{children}
</div>
),
},
});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" } }
]
}
}React schema采用元素树格式:
json
{
"root": {
"type": "Card",
"props": { "title": "Hello" },
"children": [
{ "type": "Button", "props": { "label": "Click me" } }
]
}
}Contexts
上下文
| Context | Purpose |
|---|---|
| Provide data for binding ( |
| Provide action handlers |
| Form validation state |
| Conditional rendering |
| 上下文 | 用途 |
|---|---|
| 提供绑定数据( |
| 提供动作处理器 |
| 表单验证状态 |
| 条件渲染 |
Key Exports
核心导出项
| Export | Purpose |
|---|---|
| Create a type-safe component registry from a catalog |
| Render a spec using a registry |
| Element tree schema |
| Access data context |
| Access actions context |
| 导出项 | 用途 |
|---|---|
| 从组件目录创建类型安全的组件注册表 |
| 使用注册表渲染规格 |
| 元素树Schema |
| 访问数据上下文 |
| 访问动作上下文 |