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";
// 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>
);
}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
Visibility Conditions
可见性条件
Use on elements. Syntax: , , , for AND.
visible{ "$state": "/path" }{ "$state": "/path", "eq": value }{ "$state": "/path", "not": true }[ cond1, cond2 ]在元素上使用属性。语法:、、,使用表示逻辑与。
visible{ "$state": "/path" }{ "$state": "/path", "eq": value }{ "$state": "/path", "not": true }[ cond1, cond2 ]Pressable + setState Pattern
Pressable + setState 模式
Use with the built-in action for interactive UIs like tab bars:
PressablesetStatejson
{
"type": "Pressable",
"props": {
"action": "setState",
"actionParams": { "statePath": "/activeTab", "value": "home" }
},
"children": ["home-icon", "home-label"]
}将与内置的动作搭配使用,可实现标签栏等交互UI:
PressablesetStatejson
{
"type": "Pressable",
"props": {
"action": "setState",
"actionParams": { "statePath": "/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 state model (one-way read)
{ "$state": "/state/key" } - - two-way binding: use on the natural value prop (value, checked, pressed, etc.) of form components.
{ "$bindState": "/path" } - - two-way binding to a repeat item field. Use inside repeat scopes.
{ "$bindItem": "field" } - - conditional value
{ "$cond": <condition>, "$then": <value>, "$else": <value> }
json
{
"type": "TextInput",
"props": {
"value": { "$bindState": "/form/email" },
"placeholder": "Email"
}
}Components do not use a prop for two-way binding. Use on the natural value prop instead.
statePath{ "$bindState": "/path" }任意属性值都可以是数据驱动的表达式,在渲染时实时解析:
- - 从状态模型读取数据(单向读取)
{ "$state": "/state/key" } - - 双向绑定:用于表单组件的原生值属性(value、checked、pressed等)
{ "$bindState": "/path" } - - 与循环渲染的条目字段双向绑定,需在循环作用域内使用
{ "$bindItem": "field" } - - 条件返回值
{ "$cond": <condition>, "$then": <value>, "$else": <value> }
json
{
"type": "TextInput",
"props": {
"value": { "$bindState": "/form/email" },
"placeholder": "Email"
}
}组件无需使用属性实现双向绑定,直接在原生值属性上使用即可。
statePath{ "$bindState": "/path" }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": { "statePath": "/activeTab", "value": "home" } }setStateActionProviderjson
{ "action": "setState", "actionParams": { "statePath": "/activeTab", "value": "home" } }Providers
提供者
| Provider | Purpose |
|---|---|
| Share state across components (JSON Pointer paths). Accepts optional |
| Handle actions dispatched from components |
| Enable conditional rendering based on state |
| Form field validation |
| 提供者 | 用途 |
|---|---|
| 跨组件共享状态(JSON Pointer路径)。接受可选的 |
| 处理组件派发的动作 |
| 支持基于状态的条件渲染 |
| 表单字段校验 |
External Store (Controlled Mode)
外部存储(受控模式)
Pass a to (or / ) to use external state management:
StateStoreStateProviderJSONUIProvidercreateRenderertsx
import { createStateStore, type StateStore } from "@json-render/react-native";
const store = createStateStore({ count: 0 });
<StateProvider store={store}>{children}</StateProvider>
store.set("/count", 1); // React re-renders automaticallyWhen is provided, and are ignored.
storeinitialStateonStateChange向(或 / )传入即可使用外部状态管理:
StateProviderJSONUIProvidercreateRendererStateStoretsx
import { createStateStore, type StateStore } from "@json-render/react-native";
const store = createStateStore({ count: 0 });
<StateProvider store={store}>{children}</StateProvider>
store.set("/count", 1); // React会自动重新渲染当提供时,和会被忽略。
storeinitialStateonStateChangeKey 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 state context |
| Get single value from state |
| Two-way state binding via |
| (deprecated) Legacy two-way binding by path |
| Access actions context |
| Get a single action dispatch function |
| Stream specs from an API endpoint |
| Create a framework-agnostic in-memory |
| Interface for plugging in external state management |
| 导出项 | 用途 |
|---|---|
| 基于组件目录创建类型安全的组件注册表 |
| 使用注册表渲染指定规范 |
| React Native元素树结构规范 |
| 所有标准组件的目录定义 |
| 标准动作的目录定义 |
| 预构建的组件实现 |
| 为标准动作创建处理函数 |
| 访问状态上下文 |
| 从状态中获取单个值 |
| 通过 |
| (已废弃) 旧版按路径实现的双向绑定 |
| 访问动作上下文 |
| 获取单个动作派发函数 |
| 从API端点流式获取界面规范 |
| 创建框架无关的内存级 |
| 用于接入外部状态管理的接口 |