--- url: https://bonniss.github.io/react-headless-form/guide/start/introduction.md --- # Introduction ![](https://bonniss.github.io/react-headless-form/static/image/logo.bccf764dd5.png) :::info A naming note This package was originally named `blueform`, but npm rejected the publication because the name was deemed too similar to an existing package. We then switched to **`react-headless-form`** — a clear and descriptive name, though somewhat verbose. Unfortunately, abbreviating it as “RHF” isn’t practical, as that acronym is almost universally associated with **React Hook Form** (for the skeptics — just try googling “RHF”). For brevity, this documentation refers to the package simply as **BlueForm**, while **RHF** always refers to **React Hook Form**. ::: ## Why not just use React Hook Form? React Hook Form (RHF) is brilliant. It removes a large amount of boilerplate and makes building forms faster and easier. However, as applications grow, the patterns used to wire RHF itself often become the next layer of boilerplate. You copy setup patterns from previous projects, yet still hesitate over questions like: - Should this field use `register`, `useController`, or ``? - Should we introduce helpers to separate logic from layout? - Should inputs be wrapped in context-aware components? Each project tweaks the pattern slightly, and those tweaks accumulate. Individually, these decisions are trivial. Collectively, they create architectural noise. Input wiring should not be the dominant concern in a form system. It should fade into the background, so teams can focus on structure, composition, and behavior — the parts that actually define how a form works as a whole. ## The Headless Approach With **BlueForm**, building a form becomes a clear process: ### 1. Define your fields (optional, incremental) Register reusable building blocks through `fieldMapping`, which maps a type name to a React component. The mapping is entirely yours. This step is optional, as **BlueForm** includes built-in primitives that are sufficient for many use cases, allowing you to start immediately and extend incrementally as your design system grows. ### 2. Define how your root form element is rendered Decide how the form itself is structured: form, grid layout, wizard container, or anything else. ### 3. Describe your form as configuration Compose your form using your own fields, validation rules, layout-related properties, and built-in structural types. ### 4. Define form behavior Handle submission, side effects, conditional visibility, and integrations—without coupling them to layout. And that's it — you now have a working form. The reason step **1** comes first is intentional. In most applications, fields are defined far less frequently than forms themselves. You typically know the input shapes your domain requires, and once those field types are established, they are reused across many forms. Similarly, step **2** is often optional. Many applications share the same root form structure, meaning you define it once and rarely touch it again. With **BlueForm**, you focus on form structure — how fields are organized, how they relate to each other, and how the form behaves as a whole. UI becomes an implementation detail, not the driving concern. ## ✨ Features - 🧩 **Bring your own UI** — `value`, `onChange`, `label`, and form field essentials at your fingertips, without fighting RHF or TypeScript. You define your own field types: `text`, `select`, or even a `superman` field. - ⚡ **Great DX** — set up the form once, get full type and prop hints from your TypeScript model, plus extra UI-only fields with zero TypeScript complaints. - 🧠 **Powerful Composition Model** — build forms from reusable schema blocks and assemble them like Lego into workflows of any complexity. - 🌍 **i18n-ready** — plug in any i18n solution (`i18next`, `react-intl` or your own), configure once, and labels, descriptions, and validation messages ready to fields without caring about the app language. - 🛠️ **Extensible Form Root** — Empower your form shell by easily injecting DevTools, status bars, error summaries, or any custom logic via `renderRoot`. - 🧱 **Still just [React Hook Form](https://react-hook-form.com/)** — pass `useForm` options, access RHF hooks, and keep full control since fields live inside the form context. - 📱 **Platform-agnostic** — no DOM assumptions in the engine; see the [React Native Web demo](https://bonniss.github.io/react-headless-form/examples/recipes/integrations/react-native-web.md). --- url: https://bonniss.github.io/react-headless-form/guide/start/quick-start.md --- # Quick start ## Installation ```sh [npm] npm install react-headless-form react-hook-form ``` ```sh [yarn] yarn add react-headless-form react-hook-form ``` ```sh [pnpm] pnpm add react-headless-form react-hook-form ``` ```sh [bun] bun add react-headless-form react-hook-form ``` ```sh [deno] deno add npm:react-headless-form npm:react-hook-form ``` Peer dependencies: `react-hook-form` `^7.55.0` and `react` / `react-dom` `^16.8 || ^17 || ^18 || ^19`. Install `react-hook-form` alongside BlueForm. ## Basic usage Let's start with a simple login form. The form data is represented by the following TypeScript type: ```tsx type LoginData = { username: string; password: string; }; ``` ### 0. Define a field ```tsx file="../../../components/InputField.tsx" import type { InputHTMLAttributes } from "react"; import { useField } from "react-headless-form"; export default function InputField(props: InputHTMLAttributes) { const { value, onChange, onBlur, ref, label, description, errorMessage, required, disabled, readOnly } = useField(); return (
{/* the input inside its label: associated without any id */} {description &&
{description}
} {errorMessage &&
{errorMessage}
}
); } ``` Notice that the field receives everything it needs from `useField` hook to be functional without needing to know where those values come from. ### 1. Setup the engine ```tsx import { setupForm, defineMapping } from "react-headless-form" import InputField from "./InputField" export const [Form] = setupForm({ fieldMapping: defineMapping({ text: InputField, }), }) ``` ### 2. Build the form ```tsx const BasicForm = () => { return ( renderRoot={({ children, onSubmit }) => (
{children}
)} onSubmit={(data) => alert(JSON.stringify(data, null, 2))} config={{ username: { type: "text", label: "Your name", rules: { required: "Name is required", }, }, password: { type: "text", label: "Password", rules: { required: "Password is required", }, props: { // html input type type: "password", }, }, }} > ); }; ``` ### Preview :::info Preview Your name *Password *Submit ::: :::note The form above is unstyled intentionally. BlueForm is headless. Styling is entirely up to you. ::: ## Next steps - Read [Common pitfalls](https://bonniss.github.io/react-headless-form/guide/start/pitfalls.md) — a two-minute checklist of things that trip people up. - Learn the [`
` props](https://bonniss.github.io/react-headless-form/guide/core/setup-form/form.md) and the full field config. --- url: https://bonniss.github.io/react-headless-form/guide/start/pitfalls.md --- # Common pitfalls A checklist of behaviors that are easy to get wrong. Each item links to the full explanation. ## Setup **1. `renderRoot` is required.** There is no default ``. Provide it once in `setupForm` (or per ``), otherwise rendering throws. ```tsx const [Form] = setupForm({ renderRoot: ({ children, onSubmit }) => {children}
, }) ``` **2. Install `react-hook-form` yourself.** Since v0.14 it is a peer dependency: `pnpm add react-headless-form react-hook-form`. Hooks such as `useFormContext` / `useWatch` can then be imported from either package — they share the same context. **3. `fieldMapping` is fixed at setup time.** It cannot be overridden per `
`, so that field types and their `props` stay type-checked. Other `setupForm` options (`renderRoot`, `i18nConfig`, `formOptions`) can be overridden per form. A schema goes in the per-form `schema` prop (or a `resolver` in the per-form `formOptions`). ## Values and validation **4. Validation runs on blur, then on change.** BlueForm defaults to `mode: "onTouched"` and `reValidateMode: "onChange"` (RHF's default is `onSubmit`). An adapter that never calls `onBlur` is never touched, so its errors only appear after submit: call `onBlur`. Override with `formOptions.mode`. **5. Values start as `undefined`.** Unless you pass `defaultValues` (form) or `defaultValue` (field), a field's `value` is `undefined`. Controlled inputs should render `value ?? ""` to avoid React's uncontrolled→controlled warning. **6. `rules` are ignored with a schema.** With the `schema` prop or `formOptions.resolver`, every field-level `rules` entry is disabled (a console warning is logged). Pick one: rules or schema. The `*` marker comes from `rules.required`, so with a schema set `required: true` (or a condition) on the field. See [Schema validation](https://bonniss.github.io/react-headless-form/guide/core/schema-validation.md). **7. Hidden is not the same as not validated.** A field hidden via `visible` keeps its registered value and rules unless it never mounted or `shouldUnregister` is on. For conditional requirements use `rules.validate` and a `required` function for the `*` marker. See [Conditional rendering](https://bonniss.github.io/react-headless-form/guide/core/conditional-rendering.md). **8. A field hidden from the first render never registers its `defaultValue`.** Put initial values that must always be submitted in the form-level `defaultValues`. ## Submit and change hooks **9. `onSubmit` errors go to `onSubmitFailure`, validation errors to `onInvalid`.** The old `onSubmitError` only ever fired on validation errors (it is now a deprecated alias of `onInvalid`). Errors thrown by `onSubmit` are passed to `onSubmitFailure` — or logged — and never become unhandled rejections. See [Form props](https://bonniss.github.io/react-headless-form/guide/core/setup-form/form.md#submit-lifecycle). **10. Debounced changes are flushed on unmount.** With `debounceMs`, the last pending `onFieldChange` / `onFormChange` call runs when the form unmounts; call `ref.current.flushChanges()` to run it earlier. Use `useFormRef()` for a typed ref. ## i18n **11. Only `label`, `description`, `props.placeholder` and error messages are translated.** Other field `props` (`title`, `aria-label`, select options) are passed through untouched: translate them with your `t` where you build the config. **12. Every string label is treated as a translation key.** Wrap text that is not a key (e.g. from a database) as `{ text: "..." }` so `t` is skipped. See [i18n](https://bonniss.github.io/react-headless-form/guide/core/i18n.md). ## Performance **13. `visible` / `disabled` / `required` functions only react to values read through their `values` argument.** Since v0.16, BlueForm records those reads and re-runs a function only when they change (deps are inferred; `deps` is optional). A function that reads something else, such as `getValues()` from a closure or a mutable external store, is not re-run when that changes. Read form values from the argument, keep other state in React state (so the parent re-renders), or declare `deps`. See [Conditional rendering → deps](https://bonniss.github.io/react-headless-form/guide/core/conditional-rendering.md#performance-deps). **14. With `shouldUnregister: true`, React StrictMode can drop the values of array rows added with `append()`.** StrictMode's simulated unmount / remount makes react-hook-form register the new row again from `defaultValues`, so its appended values are lost (development only). Keep `shouldUnregister` off for forms with arrays, or test that flow outside StrictMode. **15. `append()` focuses the new row by default.** If that row has required fields, clicking "Add" again blurs it first: with `mode: "onBlur"` / `"onTouched"` its error appears, the layout shifts and the second click can miss the button. Pass `append(row, { shouldFocus: false })` for repeated adds. --- url: https://bonniss.github.io/react-headless-form/guide/start/migration.md --- # Migration and deprecations ## Deprecated, removed in 1.0 Each still works and is marked `@deprecated` (or documented as an alias), so editors flag it. | Deprecated | Use instead | Since | | ----------------------- | -------------------------------------------------------------------------------------------------------- | ----- | | `onSubmitError` | `onInvalid` (validation errors); `onSubmitFailure` for errors thrown by `onSubmit` | 0.16 | | `useArrayField().items` | `fields` (marked `@deprecated` in 0.21) | 0.16 | | `useArrayField().push` | `append` (marked `@deprecated` in 0.21) | 0.16 | | `useField().id` | your own id: `useId()` in the adapter, or the input inside its `