@fieldia/react

中文文档
0.36.1发布于 2026/10/28MITTypeScriptESM + CJS近 30 天下载 4186

React binding for Fieldia: over the plain-DOM viewer, with custom fields and slots as React components.

原始使用文档

@fieldia/react

Fieldia forms in React 18 and 19: a thin shell over @fieldia/viewer, so a page looks and behaves the same as in any other framework.

npm install @fieldia/react
import { FieldiaForm } from '@fieldia/react';
import { createMemoryDataSource } from '@fieldia/core';
import page from './customer.page.json';

const dataSource = createMemoryDataSource();

export function Customer() {
  return <FieldiaForm page={page} dataSource={dataSource} recordId={1} skin="underline" />;
}

In Next (or any server-rendered React), give the component firstPaint: it renders the form's first paint on the server — its values in plain HTML, in Fieldia's look — which the browser shows before any script and hydrates as it is; the viewer then mounts over it once drawn and loaded, keeping what was typed meanwhile (@fieldia/viewer/server).

// app/enquiries/[id]/page.tsx — a server component loads the record…
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
  const record = await loadEnquiry((await params).id);
  const nonce = (await headers()).get('x-nonce') ?? undefined; // with a strict Content-Security-Policy
  return <Enquiry record={record} nonce={nonce} />;
}

// enquiry.tsx — …and the client component paints it first.
'use client';
export function Enquiry({ record, nonce }: { record: Values; nonce?: string }) {
  return <FieldiaForm page={page} dataSource={dataSource} recordId={record.id as number} nonce={nonce} firstPaint={{ values: record }} />;
}

Custom fields and slot content can be React components (fieldTypes, slots); useFormState(form) gives you a form's state as React state. A list page opens a row through onOpenRecord.

The form's events are props, each read anew on every render without mounting again; onAction may answer a button or a call step:

<FieldiaForm
  page={page}
  dataSource={dataSource}
  pages={pages}
  onAction={async ({ action, values }) => (action === 'check_stock' ? { values: { price: await priceOf(values.product) } } : undefined)}
  onChange={({ field, value, by }) => {}}   // by: 'person' | 'step' | 'app'
  onSave={({ recordId, values }) => {}}
  onSend={({ values }) => {}}
  onStep={({ step }) => {}}
  onRun={({ id, result }) => {}}
/>

host and onOpen are the viewer's own options (see @fieldia/viewer); the handle from ref or onReady has on, setValues and run.

A page with continueLater opens a draft kept on your server by its key — resume={new URLSearchParams(location.search).get('resume') ?? undefined}, the key the link Continue later gave — and onDraft={({ key, later, saved }) => …} is told of each draft kept. A new resume opens that draft, as a new recordId opens that record.

A page that fills fields from its link (prefill) reads the browser's address; an app rendered on a server, or shown inside another page, gives the link it knows — url="https://homes.example/enquiry?agent=mona&unit=B-12", its ?… part, or url={{ agent: 'mona' }}. A new url starts the form from it again.

Before your app leaves the form, ask its one check: await handle.form.leave() before a route change, before unmounting <FieldiaForm>, and before changing a prop that mounts it again (page, form, dataSource, recordId, resume, url, user, context, dir, labels, fieldTypes, slots, the names in functions). React unmounts at once, so nothing else can keep the person there. It waits for a save on its way, then asks "Discard your changes?" only when their work is at risk, and answers false when they choose to stay. warnOnLeave has the browser's own Back, Reload or a closed tab ask first while there is a change not saved.

A field's or a slot's component that throws as it renders is kept to its place: it shows a small box that names it, onError={(error, where) => …} is told, and the rest of the form stays mounted — React would otherwise take down everything under the nearest error boundary, the form with it. A page the viewer refuses says why in the form's place, onError is told, and the app stays mounted: nothing is thrown into React. MIT licensed.

版本与发布记录

@fieldia/react 共发布 50 个版本,首次发布于 2026/10/02;过去 12 个月发布了 50 个版本。

版本发布日期距今
0.36.1latest2026/10/2817 天前
0.36.02026/10/2817 天前
0.35.32026/10/2818 天前
0.35.22026/10/2718 天前
0.35.12026/10/2619 天前
0.35.02026/10/2619 天前

关于 @fieldia/react 的常见问题

@fieldia/react 是什么?

React binding for Fieldia: over the plain-DOM viewer, with custom fields and slots as React components.

如何安装 @fieldia/react?

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

@fieldia/react 支持 TypeScript 吗?

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

@fieldia/react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fieldia/react 可以商用吗?

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

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

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

@fieldia/react 还在维护吗?

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