Skip to Content
Connect software

@insane-forms/react

A Form component and a useSubmit hook for React 19 that send Submissions to an Insane Forms Form, with Files and spam protection.

@insane-forms/react gives a React 19 app a <Form> component and a useSubmit hook. Both are built on @insane-forms/core. They render no styles and no class names, and they render no error text. You supply the markup.

Install

npm install @insane-forms/react

The package needs react 19 or later as a peer dependency and installs @insane-forms/core with it. Its entry point is marked "use client", so you can import it from a Server Component file and render <Form> as a client component.

Send a Submission with Form

<Form> renders a native <form method="post" enctype="multipart/form-data"> that posts to your Form endpoint. Before JavaScript loads, or with JavaScript off, the browser posts the form and follows the server's redirect. With JavaScript, <Form> sends the Submission through the client, shows its state and then navigates to the redirect.

import { Form } from "@insane-forms/react";

export function ContactForm() {
  return (
    <Form endpoint="https://us.insaneforms.com/f/<form-id>">
      {({ status, error }) => (
        <>
          <input name="email" type="email" required />
          <textarea name="message" required />
          <input name="resume" type="file" />
          {/* Honeypot: people never see it, bots fill it in. */}
          <input
            name="_gotcha"
            type="text"
            tabIndex={-1}
            autoComplete="off"
            aria-hidden="true"
            style={{ position: "absolute", left: "-9999px" }}
          />
          {error && <p role="alert">{error}</p>}
          <button disabled={status === "submitting"}>Send</button>
        </>
      )}
    </Form>
  );
}

The field names (email, message, resume) are yours. A Form has no declared fields and accepts whatever you send. The reserved names are listed in the form endpoint reference.

Props

Form accepts every prop of a <form> element except action, method, encType, children and onError, and adds these:

PropTypeDescription
endpointstringThe full, absolute Form endpoint, https://us.insaneforms.com/f/<form-id>. Required.
onSuccess(result: SubmitSuccess) => boolean | undefinedRuns after the server accepts the Submission. Return false to stay on the page. Any other return value sends the Visitor to result.redirect.
onError(message: string) => voidRuns with the message to show when a send fails or is blocked.
childrenReactNode | (state: SubmitState) => ReactNodeMarkup, or a function that receives the submit state.

SubmitSuccess is { ok: true; id: string; redirect: string }, the success case of core's SubmitResult.

The <form> element carries data-state (idle, submitting, success or error) and sets aria-busy while sending, so you can style the states from CSS. Focusing any field warms up the Form's spam protection ahead of the send. On submit, <Form> reads the fields once. Edits made while the send waits for its checks are not sent, and the name and value of the button the Visitor pressed are part of the Submission.

Success and error states

By default a successful send navigates to result.redirect. To show your own confirmation instead, return false from onSuccess and read the state:

import { Form } from "@insane-forms/react";

export function Newsletter() {
  return (
    <Form
      endpoint="https://us.insaneforms.com/f/<form-id>"
      onSuccess={() => false}
    >
      {({ status, error }) =>
        status === "success" ? (
          <p role="status">Thanks, you are signed up.</p>
        ) : (
          <>
            <input name="email" type="email" required />
            {error && <p role="alert">{error}</p>}
            <button disabled={status === "submitting"}>Subscribe</button>
          </>
        )
      }
    </Form>
  );
}

The state passed to children has this shape:

type SubmitStatus = "idle" | "submitting" | "success" | "error";

type SubmitState = {
  status: SubmitStatus;
  /** The message to show the Visitor while status is "error". */
  error: string | null;
  /** The last result from the server, null until one arrives. */
  result: SubmitResult | null;
};

error holds the server's message when it turns a Submission down (for example Empty submission), and the client's message for failures before the send (for example Could not upload file). See the error table in the core package.

Spam protection

The Site's settings decide what a Form checks. <Form> handles each check on its own:

  • Proof of work (ALTCHA). When the Site requires it, the client solves the challenge before sending. You add nothing.
  • Cloudflare Turnstile. When the Site has a Turnstile key, <Form> loads the Turnstile script and renders the widget inside the <form>. The widget adds the hidden cf-turnstile-response field. A send without a token stops with Complete the verification before sending the form., passed to onError and shown through error. Each token works once, so <Form> renders a new widget after every send. If the Turnstile script cannot load, the send stops with The verification could not load. Check your connection and try again. The next send mounts the widget again, so a Visitor can retry without reloading the page. When the endpoint prop changes, <Form> drops the Turnstile key found for the old endpoint and asks the new one.
  • Honeypot. Add a field named _gotcha that Visitors never see, as in the first example. A Submission with a filled _gotcha goes to Spam.

A Submission filed as Spam still returns success to the Visitor. See Spam protection.

Files

Add an <input type="file"> and the files travel with the Submission. With JavaScript, each File uploads straight to storage from the Visitor's browser, up to the Workspace's upload limit. Without JavaScript a plain post carries about 4 MB of Files in total, and a larger post is rejected. See File uploads and Limits.

Without JavaScript the spam checks also cannot run. If the Site uses proof of work or Turnstile, a post without JavaScript still redirects the Visitor, but the Submission goes to Spam.

useSubmit

Use useSubmit when you render the <form> yourself.

function useSubmit(
  endpoint: string,
  options?: { onTurnstileSiteKey?: (siteKey: string) => void },
): {
  status: SubmitStatus;
  error: string | null;
  result: SubmitResult | null;
  submit: (input: FormData | HTMLFormElement) => Promise<SubmitResult>;
  prepare: () => Promise<void>;
};

submit never rejects. Every failure comes back as { ok: false, error } and sets status to "error". It does not navigate, so you decide what happens after { ok: true }. If a send is already running, a second call to submit joins it instead of starting another.

prepare loads the Form's spam protection ahead of time and rejects if that fails. Await it before you check for a Turnstile token, so a Form that needs Turnstile is never treated as one that does not.

onTurnstileSiteKey runs when the Form needs Turnstile. useSubmit does not render the widget. Render Turnstile yourself with that key inside your <form>, so the cf-turnstile-response field is part of what you pass to submit.

import { useSubmit } from "@insane-forms/react";
import type { SubmitEvent } from "react";

export function Feedback() {
  const { status, error, submit } = useSubmit(
    "https://us.insaneforms.com/f/<form-id>",
  );

  async function onSubmit(event: SubmitEvent<HTMLFormElement>) {
    event.preventDefault();
    const result = await submit(event.currentTarget);
    if (result.ok) window.location.assign(result.redirect);
  }

  return (
    <form onSubmit={onSubmit}>
      <textarea name="message" required />
      {error && <p role="alert">{error}</p>}
      <button disabled={status === "submitting"}>Send</button>
    </form>
  );
}

Exports

Form, useSubmit and the types FormProps, SubmitSuccess, SubmitState, SubmitStatus and UseSubmitOptions.

On this page