React Components

Overview

React components and hooks for building forms with Formr. Provides pre-built components and hooks that handle form submission, validation, and error handling.

Installation

npm install formr-ui
# or
pnpm add formr-ui
# or
yarn add formr-ui

Requires React 18+

Quick Start

import { useFormr, Form, Field, Errors } from 'formr-ui';

export default function ContactForm() {
  const { submit, errors, pending } = useFormr({
    formId: 'contact',
    apiKey: process.env.NEXT_PUBLIC_FORMR_KEY,
  });

  return (
    <Form onSubmit={submit}>
      <Field 
        name="email" 
        type="email" 
        label="Email" 
        required 
      />
      {errors.email && <Errors field="email" message={errors.email} />}
      
      <Field 
        name="message" 
        type="textarea" 
        label="Message" 
        required 
      />
      
      <button disabled={pending}>
        {pending ? 'Submitting...' : 'Send'}
      </button>
      
      {errors._form && <Errors message={errors._form} />}
    </Form>
  );
}

Using Hooks Directly

Components

<Form>

Wrapper component that handles form submission.

Props:

  • onSubmit - Submit handler function

  • All standard HTML form props supported

Example:

<Field>

Input field component.

Props:

  • name - Field name (required)

  • label - Field label

  • type - Input type (text, email, number, textarea, etc.)

  • All standard HTML input props supported

Example:

<Errors>

Error display component.

Props:

  • field - Field name to display errors for

  • message - Error message to display

  • errors - Error object from useFormr

Example:

Hook: useFormr(options)

React hook for form management.

Options

  • formId - Form ID (required)

  • apiKey - API key (optional)

  • baseUrl - Base URL (optional)

  • sessionId - Session ID (optional)

Returns

  • submit(data) - Submit function

  • errors - Error object

  • pending - Loading state

  • reset() - Reset errors and pending state

Example

Plain HTML Enhancement

Add the browser script to any HTML form:

The script will:

  • Intercept form submissions

  • Validate client-side (optional, UX only)

  • Submit via Formr API

  • Handle success/error states

Examples

Basic Form

Custom Styled Form

Using Hook Without Components

Features

  • Light Client-side Validation: Required/min/max/pattern/enum for instant UX

  • Server Validates Everything: Client checks are advisory, server is authoritative

  • Unstyled Primitives: Bring your own styles

  • TypeScript Support: Full TypeScript definitions

  • Tree-shakeable: Only import what you need

Next Steps

  • Development Tools - Other development tools

  • SDK Guide - JavaScript/TypeScript SDK

  • API Reference - Complete API documentation

Last updated