Add A Contact Form To NextJs
Here is guide for integrating Fabform with a Next.js site:
Step 1: Create a new Next.js site
If you don't have a Next.js site set up yet:
-
Create Next.js App: Run the following command to create a new Next.js app. You'll be prompted to name your app:
bash yarn create next-app -
Navigate to Your App: Move into the newly created app directory:
bash cd your-app-name -
Start Development Server: Launch the Next.js development environment:
bash yarn dev
Next.js will start a hot-reloading development environment accessible at http://localhost:3000/.
Step 2: Add a Contact Section to your Next.js site
-
Create Contact Page: Inside the
pagesdirectory of your Next.js app, create a new JavaScript file namedcontact.js. -
Update Contact Page Content: Replace the content of
contact.jswith the following code block:```javascript import React, { useState } from "react";
export default function Contact() { const [query, setQuery] = useState({ name: "", email: "", message: "" });
const handleParam = (e) => { const { name, value } = e.target; setQuery(prevState => ({ ...prevState, [name]: value })); };
const formSubmit = (e) => { e.preventDefault(); const formData = new FormData(); Object.entries(query).forEach(([key, value]) => { formData.append(key, value); }); fetch("https://fabform.io/f/{your-form-endpoint}", { method: "POST", body: formData }).then(() => setQuery({ name: "", email: "", message: "" })); };
return (
); } ```Contact Us
Note: Replace
{your-form-endpoint}with the unique form endpoint provided by Fabform.
Step 3: Run your Next.js site locally to finalize setup
-
Start Next.js Development Server: Run the following command to see your Next.js form in action at
localhost:3000/contact/:bash yarn dev