Next.JS

How to Use React Hook Form to Submit a Form in Next.js

How to use React Hook Form to create and submit a form in a Next.js application

10/24/2024
0 views
contactForm.tsxTypeScript
"use client";

import { useState } from "react";
import { useForm } from "react-hook-form";

const ContactForm = () => {
  const { register, reset, handleSubmit } = useForm();
  const [msg, setMsg] = useState<string>();

  const dataSubmit = (data: any) => {
    if (data) {
      setMsg("Thank You For Contacting US!");
      reset();
    }
  };

  return (
    <form className="mt-5" onSubmit={handleSubmit(dataSubmit)}>
      <div className="flex flex-col md:flex-row gap-3 md:gap-6 my-3">
        <div className="w-full md:w-1/2">
          <input
            type="text"
            placeholder="First Name"
            className="w-full text-lg px-4 py-1.5 rounded shadow-md outline-none"
            {...register("firstName", { required: true })}
          />
        </div>
        <div className="w-full md:w-1/2">
          <input
            type="text"
            placeholder="Last Name"
            className="w-full text-lg px-4 py-1.5 rounded shadow-md outline-none"
            {...register("lastName", { required: true })}
          />
        </div>
      </div>

      <div className="flex flex-col md:flex-row gap-3 md:gap-6 my-3">
        <div className="w-full md:w-1/2">
          <input
            type="email"
            placeholder="E-mail"
            className="w-full text-lg px-4 py-1.5 rounded shadow-md outline-none"
            {...register("email", { required: true })}
          />
        </div>
        <div className="w-full md:w-1/2">
          <input
            type="tel"
            placeholder="Contact No."
            className="w-full text-lg px-4 py-1.5 rounded shadow-md outline-none"
            {...register("contactNo")}
          />
        </div>
      </div>

      <div className="my-3">
        <textarea
          className="w-full text-lg px-4 py-1.5 rounded shadow-md outline-none min-h-32"
          {...register("message")}
          placeholder="Message"
        ></textarea>
      </div>

      <div className="flex items-center justify-end mt-6">
        <p className="text-white font-medium text-lg mr-4">{msg}</p>
        <button
          type="submit"
          className="bg-white rounded text-black hover:bg-green transition-all ease-linear py-2 px-3 font-medium uppercase shadow-md"
        >
          Send Message
        </button>
      </div>
    </form>
  );
};

export default ContactForm;
React Hook FormNextjsTailwindCSSContact FormWeb Design

Loading comments...

Related Examples

How to Create Scrolling Transition on Header Using Next.js 15 and Tailwind CSS
Next.JS

How to Create Scrolling Transition on Header Using Next.js 15 and Tailwind CSS

How to Set Up a Next.js 15 Application: A Step-by-Step Guide
Next.JS

How to Set Up a Next.js 15 Application: A Step-by-Step Guide

How to Deploy a Next.js Application on Vercel
Next.JS

How to Deploy a Next.js Application on Vercel

How to Highlight a Menu on Scroll of the Page Using Next.js and TailwindCSS
Next.JS

How to Highlight a Menu on Scroll of the Page Using Next.js and TailwindCSS

How to Create a Carousel in Next.js Using Slick-Carousel
Next.JS

How to Create a Carousel in Next.js Using Slick-Carousel

How to Optimize Images in Next.js - Speed Up Your Website
Next.JS

How to Optimize Images in Next.js - Speed Up Your Website

How to Create an Image Gallery using Next.js and TailwindCSS
Next.JS

How to Create an Image Gallery using Next.js and TailwindCSS

How to Iterate Array Object for Travel Package List Using React.js
Next.JS

How to Iterate Array Object for Travel Package List Using React.js