Skip to content
Lazyform
Docs menu

Developers

React hook

useLazyform() gives any React form a submit handler and a status. Works with Next.js, Vite, Remix, Lovable, Bolt and v0.

The package isn't on npm yet. Until it is, use fetch or the drop-in widget, which work today with the same endpoint.

bash
npm install lazyform

useLazyform()

tsx
import { useLazyform } from 'lazyform/react'

export function ContactForm() {
  const { handleSubmit, status, error, reset } = useLazyform('YOUR_ACCESS_KEY')

  if (status === 'success') {
    return (
      <p>
        Thanks! Your message was sent. <button onClick={reset}>Send another</button>
      </p>
    )
  }

  return (
    <form onSubmit={handleSubmit}>
      <input type="checkbox" name="botcheck" style={{ display: 'none' }} tabIndex={-1} autoComplete="off" />
      <label htmlFor="name">Name</label>
      <input id="name" name="name" />
      <label htmlFor="email">Email</label>
      <input id="email" type="email" name="email" required />
      <label htmlFor="message">Message</label>
      <textarea id="message" name="message" required />
      <button type="submit" disabled={status === 'submitting'}>
        {status === 'submitting' ? 'Sending…' : 'Send'}
      </button>
      {status === 'error' && <p role="alert">{error?.message}</p>}
    </form>
  )
}

useLazyform(accessKey, options?) returns:

Name Meaning
handleSubmit Put it on <form onSubmit>. Stops the page reload, sends every named field, clears the form on success
submit(data) Send anything yourself: an object, FormData or a form element. Resolves to the result
status 'idle', 'submitting', 'success' or 'error'
error { code, message, status? } when status is 'error', else null
result { id, message } when status is 'success', else null
reset() Back to 'idle', e.g. to show the form again

The hook remembers when the form first rendered and sends it as _lf_started, so the time trap works without extra code. While a submission is in flight, more calls return that same submission, so double clicks send once.

Options

Option Default Meaning
onSuccess(result) none Called after a successful submission (redirect, analytics, toast)
onError(error) none Called after a failed one
resetOnSuccess true Clear the form after handleSubmit succeeds
endpoint https://api.lazyform.dev/submit Where to post
fetch global fetch Your own fetch

Controlled inputs and form libraries

handleSubmit reads the form's fields, so inputs need a name. If your fields live in state (react-hook-form, Formik, TanStack Form, your own useState), call submit with the values instead:

tsx
import { useForm } from 'react-hook-form'
import { useLazyform } from 'lazyform/react'

export function Signup() {
  const { register, handleSubmit, reset } = useForm()
  const lazyform = useLazyform('YOUR_ACCESS_KEY', { onSuccess: () => reset() })

  return (
    <form onSubmit={handleSubmit((values) => lazyform.submit(values))}>
      <input type="email" {...register('email', { required: true })} />
      <button disabled={lazyform.status === 'submitting'}>Join the waitlist</button>
      {lazyform.status === 'success' && <p>You're on the list.</p>}
      {lazyform.status === 'error' && <p role="alert">{lazyform.error?.message}</p>}
    </form>
  )
}

Controlled inputs aren't cleared by resetOnSuccess (React owns their values); clear your state in onSuccess.

<LazyformForm>

A <form> with the hook built in. It takes every normal form prop plus the hook's options, and children can be a function of the hook's state:

tsx
import { LazyformForm } from 'lazyform/react'

export function Newsletter() {
  return (
    <LazyformForm accessKey="YOUR_ACCESS_KEY" className="flex gap-2" onSuccess={() => console.log('subscribed')}>
      {({ status, error }) =>
        status === 'success' ? (
          <p>Thanks for subscribing!</p>
        ) : (
          <>
            <input type="email" name="email" required placeholder="you@example.com" />
            <button disabled={status === 'submitting'}>Subscribe</button>
            {error && <p role="alert">{error.message}</p>}
          </>
        )
      }
    </LazyformForm>
  )
}

It sets aria-busy while sending.

Next.js

Both exports are marked 'use client', so you can render <LazyformForm> from a server component. A component that calls useLazyform needs 'use client' at the top of its own file, like any hook. No API route or server action is needed: the browser posts straight to Lazyform.

The access key

The key is public by design: it only allows sending submissions to your form. Put it in the component, or in a public env var (NEXT_PUBLIC_LAZYFORM_KEY, VITE_LAZYFORM_KEY). To stop other sites from using it, turn on allowed websites.