跳转到内容
FFormhook
全部指南

指南

Gatsby 联系表单(无需后端)

Gatsby 构建静态 HTML,因此 Gatsby 联系表单在运行时只是普通 HTML。放入 form action,或如果您想要内联的成功和错误状态,使用一个带 fetch 的小型 React 组件。

使用原生 HTML 表单

您不需要 API 路由。表单直接 POST 到 Formhook,您的框架完全不必接触提交内容。

contact.html
<form action="https://formhook.app/f/YOUR_API_KEY" method="POST">
  <label>
    Email
    <input type="email" name="email" required />
  </label>

  <label>
    Message
    <textarea name="message" required></textarea>
  </label>

  <!-- honeypot: bots fill this; humans don't see it -->
  <input type="text" name="_gotcha" tabindex="-1" autocomplete="off" hidden />

  <!-- optional: send users to a thank-you page after a native POST -->
  <input type="hidden" name="_redirect" value="https://example.com/thanks" />

  <button type="submit">Send</button>
</form>

或在 Gatsby 中通过 fetch 提交

如果您希望在不刷新整个页面的情况下获得客户端的成功/错误状态,使用这个版本。

src/components/contact-form.tsx
// src/components/contact-form.tsx
import { useState } from "react";

export function ContactForm() {
  const [status, setStatus] = useState<"idle" | "sending" | "ok" | "error">("idle");

  async function onSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();
    setStatus("sending");

    const form = e.currentTarget;
    const data = Object.fromEntries(new FormData(form));

    const res = await fetch("https://formhook.app/f/YOUR_API_KEY", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(data),
    });

    setStatus(res.ok ? "ok" : "error");
    if (res.ok) form.reset();
  }

  if (status === "ok") return <p>Thanks - we'll be in touch.</p>;

  return (
    <form onSubmit={onSubmit}>
      <input type="email" name="email" placeholder="Email" required />
      <textarea name="message" placeholder="Message" required />
      <input type="text" name="_gotcha" tabIndex={-1} hidden />
      <button type="submit" disabled={status === "sending"}>
        {status === "sending" ? "Sending…" : "Send"}
      </button>
      {status === "error" && <p>Something went wrong. Please try again.</p>}
    </form>
  );
}

工作原理

原生表单代码片段可放入任何 Gatsby 页面或组件 - 它是纯 HTML,不需要 Gatsby 特有的数据层。基于 fetch 的 React 组件在客户端处理提交并 POST 到 formhook.app/f/YOUR_API_KEY。无需安装 gatsby-source-formhook 插件,也无需部署 serverless 函数。

之后会发生什么

  • 提交一秒内出现在您的 Formhook 仪表盘中。
  • 推送通知会在每个启用它的设备上触发。
  • 从仪表盘直接回复(Pro 及以上)- 让您真实的邮箱不出现在 Gatsby 站点的静态 HTML 中。

今天就上线您的 Gatsby 联系表单。

免费注册、创建表单、粘贴 API 密钥。五分钟搞定。

免费开始

免费层:5 个表单 · 每月 250 次提交 · 无需信用卡。

Gatsby 联系表单 - 常见问题

我需要 Gatsby Function 或 Netlify function 吗?
不需要。表单直接 POST 到 Formhook。您的 Gatsby 站点可以保持纯静态构建 - 无需 serverless function 适配器。
我能在本地使用 `gatsby develop` 时使用它吗?
可以。无论 Gatsby 如何提供 HTML,浏览器都会发起 POST。开发期间只需确保 http://localhost:8000 在表单的允许来源列表中。
如何在不添加 Gatsby 插件的情况下处理垃圾?
您不需要。honeypot 字段静默丢弃机器人,Cloudflare Turnstile 是 Formhook 仪表盘里每个表单的开关。

其他框架的指南

Migrating from Formspree? See how Formhook compares.