@declarion/react

中文文档
0.244.0发布于 2026/11/12MITTypeScriptES Module近 30 天下载 2.4万

React SDK for Declarion, the schema-driven business apps platform.

原始使用文档
文档目录6 节
  1. Install
  2. Subpath exports
  3. Icons
  4. Vite plugin: build identity for client staleness handling
  5. License
  6. Runtime

@declarion/react

React SDK for Declarion, the schema-driven business apps platform.

Declarion lets you build production business apps from YAML schemas: entities, screens, navigation, actions, and handlers. This package is the React client that renders those schemas into working UIs against a Declarion API backend.

Install

pnpm add @declarion/react
# peer deps:
pnpm add react react-dom

Subpath exports

ImportPurpose
@declarion/reactMain React entry — components, hooks, types
@declarion/react/iconsLightweight icon rendering — DynamicIcon + setIconRegistry + built-in glyphs + lazy Lucide, WITHOUT the eager UI bundle (~5 kB gzip). For host/embed apps that render nav from /api/schema.
@declarion/react/styles.cssPre-compiled Tailwind output; import once at the SPA root. Ships Inter + JetBrains Mono (SIL OFL) as sibling .woff2 files, referenced relatively - your bundler emits them.
@declarion/react/viteNode-only Vite plugin — opt into client staleness handling at build time

Icons

A DSL icon: value is a NAME resolved against Lucide (the default icon library, ~1500 glyphs) by name. Resolution order: a consumer-declared icons: entry served on /api/schema (svg / url / lucide-alias) → the curated built-in set (src/components/primitives/Icons.tsx, thin wrappers over Lucide, bundled eagerly for instant render) → Lucide by name (lazy) → folder. A host renders any icon with one component:

import { setIconRegistry, DynamicIcon } from "@declarion/react/icons";

// once, after the /api/schema fetch (it carries any custom icons):
setIconRegistry(schema.icons ?? {});
// anywhere, any depth - the registry is a module singleton:
<DynamicIcon name={navItem.icon} size={16} />

setIconRegistry is a module-level store (mirrors registerScreen); DynamicIcon subscribes via useSyncExternalStore, so replacing the registry (identity switch) re-renders every icon. Bare names resolve from Lucide - no registry entry needed. Lucide's per-icon dynamicIconImports map is loaded only via import() and stays out of the eager bundle (the curated Icons.tsx set is tree-shaken named imports). See examples/embed-demo for a working host.

The curated built-in set is just a name→Lucide-component map with the SDK's { size, cls, style } prop adapter; there is no hand-drawn SVG and no codegen. To add a name to the eager set, add an entry to Icons.tsx; anything not in it resolves lazily from Lucide regardless.

Vite plugin: build identity for client staleness handling

The SDK ships with a tiny Vite plugin that emits bundle-fingerprint.txt next to index.html after every production build. The Declarion server reads it at boot and folds it into the build identity stamped on every /api/* response (X-Declarion-Version). The SDK's built-in version watcher detects drift on every API response, on focus/online/60s probe, and either silently invalidates the schema query (schema-only changes) or prompts the user to reload (asset/binary changes). Cross-tab coordination via BroadcastChannel.

Add the plugin to your consumer app's vite.config.ts:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { declarionFingerprintPlugin } from "@declarion/react/vite";

export default defineConfig({
  plugins: [react(), declarionFingerprintPlugin()],
});

That's it — there is no client API to wire by hand. The version watcher mounts itself inside <App>, the response-header observer is built into apiFetch, and the toast slot reuses the existing Sonner <Toaster/>. The plugin works in every Declarion deployment mode (local SDK link, npm-pinned, prebuilt prod image) since it runs at consumer build time regardless of where the SDK itself comes from.

Consumers that haven't adopted the plugin yet degrade gracefully: the server logs once at boot, falls back to binary-only drift detection, and the rest of the system continues working — drift still gets detected, just classified more conservatively as "asset/binary" so the user gets a reload prompt rather than a silent invalidation.

Browser support requires BroadcastChannel for cross-tab coordination — Chrome 54+, Firefox 38+, Safari 15.4+. In environments without it, in-tab detection still works; cross-tab fan-out is silently skipped.

License

MIT. See LICENSE.

Runtime

The SDK talks to a Declarion runtime server. The runtime is proprietary commercial software - contact [email protected] for licensing.

版本与发布记录

@declarion/react 共发布 287 个版本,首次发布于 2026/04/16;过去 12 个月发布了 287 个版本。

版本发布日期距今
0.244.0latest2026/11/12今天
0.241.02026/11/11今天
0.240.02026/11/11今天
0.239.02026/11/11今天
0.238.02026/11/11今天
0.237.02026/11/11今天

关于 @declarion/react 的常见问题

@declarion/react 是什么?

React SDK for Declarion, the schema-driven business apps platform.

如何安装 @declarion/react?

在项目目录运行 npm install @declarion/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^19.0.0)、react-dom(^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@declarion/react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@declarion/react 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@declarion/react 可以商用吗?

@declarion/react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@declarion/react 的下载量有多少?

根据 npm 官方下载统计,@declarion/react 在 2026/10/12 – 2026/11/10 这 30 天内被下载 23,836 次,日均约 795 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@declarion/react 还在维护吗?

最新版本 0.244.0 发布于 2026/11/12(今天);至今共发布 287 个版本,其中过去 12 个月发布了 287 个;首次发布于 2026/04/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。