json-render-react-native
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese@json-render/react-native
@json-render/react-native
React Native renderer that converts JSON specs into native mobile component trees with standard components, data binding, visibility, actions, and dynamic props.
这是一款React Native渲染器,可将JSON规范转换为包含标准组件、数据绑定、可见性控制、操作处理和动态属性的原生移动组件树。
Quick Start
快速开始
typescript
import { defineCatalog } from "@json-render/core";
import { schema } from "@json-render/react-native/schema";
import {
standardComponentDefinitions,
standardActionDefinitions,
} from "@json-render/react-native/catalog";
import { defineRegistry, Renderer, type Components } from "@json-render/react-native";
import { z } from "zod";
// Create catalog with standard + custom components
const catalog = defineCatalog(schema, {
components: {
...standardComponentDefinitions,
Icon: {
props: z.object({ name: z.string(), size: z.number().nullable(), color: z.string().nullable() }),
slots: [],
description: "Icon display",
},
},
actions: standardActionDefinitions,
});
// Register only custom components (standard ones are built-in)
const { registry } = defineRegistry(catalog, {
components: {
Icon: ({ props }) => <Ionicons name={props.name} size={props.size ?? 24} />,
} as Components<typeof catalog>,
});
// Render
function App({ spec }) {
return (
<StateProvider initialState={{}}>
<VisibilityProvider>
<ActionProvider handlers={{}}>
<Renderer spec={spec} registry={registry} />
</ActionProvider>
</VisibilityProvider>
</StateProvider>
);
}typescript
import { defineCatalog } from "@json-render/core";
import { schema } from "@json-render/react-native/schema";
import {
standardComponentDefinitions,
standardActionDefinitions,
} from "@json-render/react-native/catalog";
import { defineRegistry, Renderer, type Components } from "@json-render/react-native";
import { z } from "zod";
// 创建包含标准组件和自定义组件的目录
const catalog = defineCatalog(schema, {
components: {
...standardComponentDefinitions,
Icon: {
props: z.object({ name: z.string(), size: z.number().nullable(), color: z.string().nullable() }),
slots: [],
description: "Icon display",
},
},
actions: standardActionDefinitions,
});
// 仅注册自定义组件(标准组件已内置)
const { registry } = defineRegistry(catalog, {
components: {
Icon: ({ props }) => <Ionicons name={props.name} size={props.size ?? 24} />,
} as Components<typeof catalog>,
});
// 渲染
function App({ spec }) {
return (
<StateProvider initialState={{}}>
<VisibilityProvider>
<ActionProvider handlers={{}}>
<Renderer spec={spec} registry={registry} />
</ActionProvider>
</VisibilityProvider>
</StateProvider>
);
}Standard Components
标准组件
Layout
布局类
- - wrapper with padding, background, border radius
Container - - horizontal flex layout with gap, alignment
Row - - vertical flex layout with gap, alignment
Column - - scrollable area (vertical or horizontal)
ScrollContainer - - safe area insets for notch/home indicator
SafeArea - - touchable wrapper that triggers actions on press
Pressable - - fixed or flexible spacing
Spacer - - thin line separator
Divider
- - 带内边距、背景和圆角的容器
Container - - 带间距和对齐设置的水平弹性布局
Row - - 带间距和对齐设置的垂直弹性布局
Column - - 可滚动区域(垂直或水平)
ScrollContainer - - 适配刘海屏/底部指示条的安全区域
SafeArea - - 可触摸容器,点击时触发操作
Pressable - - 固定或自适应间距组件
Spacer - - 细线条分隔符
Divider
Content
内容类
- - heading text (levels 1-6)
Heading - - body text
Paragraph - - small label text
Label - - image display with sizing modes
Image - - circular avatar image
Avatar - - small status badge
Badge - - tag/chip for categories
Chip
- - 标题文本(1-6级)
Heading - - 正文文本
Paragraph - - 小型标签文本
Label - - 支持多种尺寸模式的图片展示组件
Image - - 圆形头像组件
Avatar - - 小型状态徽章
Badge - - 分类标签组件
Chip
Input
输入类
- - pressable button with variants
Button - - text input field
TextInput - - toggle switch
Switch - - checkbox with label
Checkbox - - range slider
Slider - - search input
SearchBar
- - 带多种样式的可点击按钮
Button - - 文本输入框
TextInput - - 开关切换组件
Switch - - 带标签的复选框
Checkbox - - 范围滑块
Slider - - 搜索输入框
SearchBar
Feedback
反馈类
- - loading indicator
Spinner - - progress indicator
ProgressBar
- - 加载指示器
Spinner - - 进度指示器
ProgressBar
Composite
复合类
- - card container with optional header
Card - - list row with title, subtitle, accessory
ListItem - - bottom sheet modal
Modal
- - 可选头部的卡片容器
Card - - 包含标题、副标题和附属元素的列表行
ListItem - - 底部弹窗组件
Modal
Pressable + setState Pattern
Pressable + setState 模式
Use with the built-in action for interactive UIs like tab bars:
PressablesetStatejson
{
"type": "Pressable",
"props": {
"action": "setState",
"actionParams": { "path": "/activeTab", "value": "home" }
},
"children": ["home-icon", "home-label"]
}结合和内置的操作,可实现标签栏等交互式UI:
PressablesetStatejson
{
"type": "Pressable",
"props": {
"action": "setState",
"actionParams": { "path": "/activeTab", "value": "home" }
},
"children": ["home-icon", "home-label"]
}Dynamic Prop Expressions
动态属性表达式
Any prop value can be a data-driven expression resolved at render time:
- - reads from data model
{ "$path": "/state/key" } - - conditional value
{ "$cond": <condition>, "$then": <value>, "$else": <value> }
json
{
"color": {
"$cond": { "eq": [{ "path": "/activeTab" }, "home"] },
"$then": "#007AFF",
"$else": "#8E8E93"
}
}Components receive already-resolved props. No changes needed to component implementations.
任何属性值都可以是在渲染时解析的数据驱动表达式:
- - 从数据模型中读取值
{ "$path": "/state/key" } - - 条件判断值
{ "$cond": <condition>, "$then": <value>, "$else": <value> }
json
{
"color": {
"$cond": { "eq": [{ "path": "/activeTab" }, "home"] },
"$then": "#007AFF",
"$else": "#8E8E93"
}
}组件会接收已解析完成的属性,无需修改组件实现代码。
Built-in Actions
内置操作
The action is handled automatically by and updates the state model directly, which re-evaluates visibility conditions and dynamic prop expressions:
setStateActionProviderjson
{ "action": "setState", "actionParams": { "path": "/activeTab", "value": "home" } }setStateActionProviderjson
{ "action": "setState", "actionParams": { "path": "/activeTab", "value": "home" } }Providers
提供者组件
| Provider | Purpose |
|---|---|
| Share state across components (JSON Pointer paths) |
| Handle actions dispatched from components |
| Enable conditional rendering based on state |
| Form field validation |
| 提供者组件 | 用途 |
|---|---|
| 在组件间共享状态(使用JSON Pointer路径) |
| 处理组件触发的操作 |
| 支持基于状态的条件渲染 |
| 表单字段验证 |
Key Exports
核心导出项
| Export | Purpose |
|---|---|
| Create a type-safe component registry from a catalog |
| Render a spec using a registry |
| React Native element tree schema |
| Catalog definitions for all standard components |
| Catalog definitions for standard actions |
| Pre-built component implementations |
| Create handlers for standard actions |
| Access data context |
| Get single value from data |
| Two-way data binding |
| Access actions context |
| Get a single action dispatch function |
| Stream specs from an API endpoint |
| 导出项 | 用途 |
|---|---|
| 从组件目录创建类型安全的组件注册表 |
| 使用注册表渲染规范内容 |
| React Native元素树的规范 schema |
| 所有标准组件的目录定义 |
| 标准操作的目录定义 |
| 预构建的组件实现 |
| 创建标准操作的处理函数 |
| 访问数据上下文 |
| 从数据中获取单个值 |
| 双向数据绑定 |
| 访问操作上下文 |
| 获取单个操作的触发函数 |
| 从API端点流式获取规范内容 |