Skip to content
Lazyform
Docs menu

Frameworks and builders

React

Build a React contact form that posts to Lazyform, with loading, success and error states. No backend needed.

This guide shows a React 19 contact form that sends submissions to Lazyform. It works the same in Vite, Create React App leftovers, or any React setup that renders in the browser.

Before you start

Get an access key: type your email on the homepage for an instant key, or sign in and create a form. Replace YOUR_ACCESS_KEY in the examples with your key (it starts with lf_).

The minimal form

The simplest version is a regular HTML form rendered by React. The browser posts it and the visitor sees a thank-you page.

ContactForm.jsx
export default function ContactForm() {
  return (
    <form action="https://lazyform-api.xvelopers.com/submit" method="POST">
      <input type="hidden" name="access_key" value="YOUR_ACCESS_KEY" />
      <input type="text" name="name" placeholder="Name" required />
      <input type="email" name="email" placeholder="Email" required />
      <textarea name="message" placeholder="Message" required />
      <input type="checkbox" name="botcheck" className="hidden" style={{ display: 'none' }} tabIndex={-1} autoComplete="off" />
      <button type="submit">Send</button>
    </form>
  );
}

React 19 treats a string action like a normal HTML form action, so this posts straight to Lazyform.

With fetch and inline messages

Most React apps keep the visitor on the page. Send JSON with Accept: application/json and read the response.

ContactForm.jsx
import { useState } from 'react';

const ENDPOINT = 'https://lazyform-api.xvelopers.com/submit';

export default function ContactForm() {
  const [status, setStatus] = useState('idle'); // idle | sending | success | error
  const [error, setError] = useState('');

  async function handleSubmit(event) {
    event.preventDefault();
    const form = event.currentTarget;
    setStatus('sending');
    setError('');

    try {
      const response = await fetch(ENDPOINT, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
        body: JSON.stringify(Object.fromEntries(new FormData(form))),
      });
      const result = await response.json();

      if (result.success) {
        setStatus('success');
        form.reset();
      } else {
        setStatus('error');
        setError(result.error?.message ?? 'Something went wrong.');
      }
    } catch {
      setStatus('error');
      setError('Network error. Please try again.');
    }
  }

  if (status === 'success') {
    return <p>Thanks! Your message has been sent.</p>;
  }

  return (
    <form onSubmit={handleSubmit}>
      <input type="hidden" name="access_key" value="YOUR_ACCESS_KEY" />
      <input type="text" name="name" placeholder="Name" required />
      <input type="email" name="email" placeholder="Email" required />
      <textarea name="message" placeholder="Message" required />
      <input type="checkbox" name="botcheck" className="hidden" style={{ display: 'none' }} tabIndex={-1} autoComplete="off" />
      <button type="submit" disabled={status === 'sending'}>
        {status === 'sending' ? 'Sending...' : 'Send'}
      </button>
      {status === 'error' && <p role="alert">{error}</p>}
    </form>
  );
}

Tips

  • Environment variables. The key is safe to ship to the browser (it only allows submitting to your form), but you can still keep it in import.meta.env.VITE_LAZYFORM_KEY to switch keys per environment.
  • Files. Skip JSON.stringify and send body: new FormData(form) with only the Accept header. See File uploads.
  • Form libraries. With React Hook Form or similar, post the validated values object as JSON in the same way, adding access_key to it.
  • Lock it down. Turn on allowed websites in the form's settings so only your domain can submit. See Spam protection.

Where to go next