Quick answer
React Hook Form is a small library that manages form state for you: you register each input, declare its rules, and it hands you the values and the errors, without re-rendering the whole form on every keystroke. This post builds a real checkout form for a Karachi clothing shop with it, validates a bad email and a quantity of zero, and proves it with a Vitest test that runs for real. Three things went wrong.
A small clothing shop in Karachi takes orders over WhatsApp. The owner reads each message, copies the name, email, city, quantity, and payment method into a notebook, works out the total and replies, and the customer waits. It works until it does not: an email typed wrong means the confirmation never arrives, a quantity of zero slips through and the shop packs nothing, and nobody can tell if an order was handled. A hand-rolled useState form has the same shape: five fields, five pieces of state, validation mixed into the submit handler. React Hook Form exists to take that bookkeeping away.
What I would check before using it
My first check is whether the form is mostly fields with rules checked at submit time. React Hook Form fits that shape: name, email, city, quantity, payment, each with a rule. If every keystroke must drive something else on the page, like a live search, controlled state fits better, and the Zustand: React State Without the Boilerplate post covers state that many components share.
My second check is how the rules are shared. The rules here live in the form itself, fine for one checkout. If the same rules must run on the server, a shared schema beats writing them twice, and the Zod: Stop Bad Data at the Door of Your Checkout post shows that pattern.
My third check is the size of the form. Five fields with real rules is where it starts paying for itself: one useState per field plus checks is more code than register calls. One email field does not need it.
My fourth check is whether the team tests forms. React Hook Form works with Testing Library and Vitest, and the Vitest Explained: Tests That Finish Before You Blink post covers the runner. An untested form rots.
My verdict, as judgement: adopt it for forms where validation matters and values are read at submit time. It flips for a single field, when every keystroke must re-render the page, or when the rules belong in a shared schema.
A Karachi checkout form, validated end to end
The demo is a checkout form for a Karachi clothing shop, for a White Cotton Kurta at Rs 2,450. The customer enters a name, email, city, quantity from 1 to 10, and payment method, and the form shows the total. It ran on Node v24.22.1 with React 19.3.1, Vite 8.4.0, and react-hook-form 7.92.0.
Scaffold the project, then add the library and the test tools:
$ npm create vite@latest rhf-checkout -- --template react $ cd rhf-checkout $ npm install $ npm install react-hook-form $ npm install -D vitest @testing-library/react @testing-library/jest-dom @testing-library/user-event jsdom The component registers five fields, each rule carrying its own message:
import { useState } from "react"; import { useForm } from "react-hook-form"; const PRICE_RS = 2450; const ITEM_NAME = "White Cotton Kurta"; function formatRs(amount) { return "Rs " + amount.toLocaleString("en-US"); } export default function CheckoutForm() { const { register, handleSubmit, watch, formState: { errors }, } = useForm({ defaultValues: { name: "", email: "", city: "", quantity: 1, payment: "Cash on delivery", }, }); const [placed, setPlaced] = useState(null); const quantity = watch("quantity"); const total = PRICE_RS * (Number.isFinite(quantity) ? quantity : 0); function onSubmit(data) { setPlaced({ ...data, item: ITEM_NAME, total_rs: PRICE_RS * data.quantity, }); } if (placed) { return ( <div> <h2>Order placed</h2> <p> Order placed for {placed.name}: {placed.quantity} x {placed.item}, total {formatRs(placed.total_rs)}, {placed.payment} to {placed.city}. </p> <p>A confirmation goes to {placed.email}.</p> </div> ); } return ( <form onSubmit={handleSubmit(onSubmit)} noValidate> <h2>Checkout</h2> <label htmlFor="name">Full name</label> <input id="name" {...register("name", { required: "Name is required" })} /> {errors.name && <p role="alert">{errors.name.message}</p>} <label htmlFor="email">Email</label> <input id="email" type="email" {...register("email", { required: "Email address is required", pattern: { value: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, message: "Please enter a valid email address", }, })} /> {errors.email && <p role="alert">{errors.email.message}</p>} <label htmlFor="city">City</label> <input id="city" {...register("city", { required: "City is required" })} /> {errors.city && <p role="alert">{errors.city.message}</p>} <label htmlFor="quantity">Quantity (1 to 10)</label> <input id="quantity" type="number" {...register("quantity", { required: "Quantity is required", valueAsNumber: true, min: { value: 1, message: "Quantity must be at least 1" }, max: { value: 10, message: "Quantity must be at most 10" }, })} /> {errors.quantity && <p role="alert">{errors.quantity.message}</p>} <label htmlFor="payment">Payment method</label> <select id="payment" {...register("payment", { required: "Payment method is required" })} > <option>Cash on delivery</option> <option>JazzCash</option> <option>Bank transfer</option> </select> {errors.payment && <p role="alert">{errors.payment.message}</p>} <p> {ITEM_NAME} at {formatRs(PRICE_RS)} each. Total: {formatRs(total)} </p> <button type="submit">Place order</button> </form> ); } The test fills the form twice: a bad email with a quantity of zero, then a valid order:
import { describe, it, expect } from "vitest"; import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import CheckoutForm from "./CheckoutForm"; describe("CheckoutForm", () => { it("shows validation errors for a bad email and a quantity of 0", async () => { const user = userEvent.setup(); render(<CheckoutForm />); await user.type(screen.getByLabelText("Full name"), "Ayesha Khan"); await user.type(screen.getByLabelText("Email"), "ayesha-at-example"); await user.type(screen.getByLabelText("City"), "Karachi"); await user.clear(screen.getByLabelText(/Quantity/)); await user.type(screen.getByLabelText(/Quantity/), "0"); await user.click(screen.getByRole("button", { name: "Place order" })); expect( screen.getByText("Please enter a valid email address") ).toBeInTheDocument(); expect( screen.getByText("Quantity must be at least 1") ).toBeInTheDocument(); }); it("places a valid order and shows the submitted data and total", async () => { const user = userEvent.setup(); render(<CheckoutForm />); await user.type(screen.getByLabelText("Full name"), "Ayesha Khan"); await user.type(screen.getByLabelText("Email"), "ayesha@example.com"); await user.type(screen.getByLabelText("City"), "Karachi"); await user.clear(screen.getByLabelText(/Quantity/)); await user.type(screen.getByLabelText(/Quantity/), "3"); await user.selectOptions( screen.getByLabelText("Payment method"), "Cash on delivery" ); await user.click(screen.getByRole("button", { name: "Place order" })); expect( screen.getByText( "Order placed for Ayesha Khan: 3 x White Cotton Kurta, total Rs 7,350, Cash on delivery to Karachi." ) ).toBeInTheDocument(); }); }); The first run failed both tests, for two reasons: the first test expected the wrong message text, and the second tripped over a leftover form:
FAIL src/CheckoutForm.test.jsx > CheckoutForm > shows validation errors for a bad email and a quantity of 0 TestingLibraryElementError: Unable to find an element with the text: Enter a valid email. FAIL src/CheckoutForm.test.jsx > CheckoutForm > places a valid order and shows the submitted data and total TestingLibraryElementError: Found multiple elements with the role "button" and name "Place order" Test Files 1 failed (1) Tests 2 failed (2) Three fixes. First, the real messages: "Please enter a valid email address" and "Quantity must be at least 1", not the shorter texts the test guessed. Second, one line in the Vitest config, globals: true, so Testing Library cleans up each form. After those two the valid-order test passed, but the error test failed. The debug run showed the email input held "ayesha-at-example" yet no error appeared: the browser's own validation for type="email" had blocked the submit before React Hook Form ran. The third fix is noValidate on the form.
The third run passed both tests, and the debug print showed the real errors and the order:
✓ src/CheckoutForm.test.jsx (2 tests) 487ms ✓ CheckoutForm (2) ✓ shows validation errors for a bad email and a quantity of 0 311ms Test Files 1 passed (1) Tests 2 passed (2) Start at 03:06:52 Duration 2.59s (environment 56%, tests 21%, setup 10%, import 8%, transform 4%) ERRORS SHOWN: ["Please enter a valid email address","Quantity must be at least 1"] ORDER LINE: "Order placed for Ayesha Khan: 3 x White Cotton Kurta, total Rs 7,350, Cash on delivery to Karachi." Rs 2,450 times 3 is Rs 7,350, and the production build passed too: 18 modules transformed, built in 370ms.
Common mistakes and how to fix them
First, forgetting noValidate on the form. The symptom is a submit that does nothing: no error messages appear, and in the browser the built-in email bubble shows instead. The fix is to add noValidate to the form so React Hook Form owns validation.
Second, asserting the wrong error text. The symptom is TestingLibraryElementError: Unable to find an element with the text: Enter a valid email. The fix is to copy the exact string from the rule in register: the test checks the words on the page, not the idea of them.
Third, tests leaking into each other. The symptom is TestingLibraryElementError: Found multiple elements with the role "button" and name "Place order", because the first test's form was still there when the second test rendered. The fix is globals: true in the Vitest config, so Testing Library cleans up after each test.
Fourth, registering a number input without valueAsNumber. The symptom is quiet: the quantity arrives as the string "3", and 2450 * "3" is 7350, so the total looks right, but "3" + 1 is "31" and "3" === 3 is false. The fix is valueAsNumber: true in register.
Frequently Asked Questions
Do I need Zod with React Hook Form? Not for a form like this. The built-in rules cover required fields, patterns, and ranges. A schema earns its place when the same rules must run on the server; the Zod: Stop Bad Data at the Door of Your Checkout post shows that setup.
Does React Hook Form work with controlled inputs? Yes, through Controller, which connects a controlled component to the form. This checkout did not need it.
Why does the form need noValidate? A type="email" input has browser validation, and the browser runs it before React Hook Form sees the submit. Without noValidate, your own messages never appear.
Can I split this checkout across pages? Yes. Keep one form state at the top, render one step at a time, and let the router handle the rest. The React Router: Pages That Change Without Reloading post covers the routing.
What to remember
- Register each input once, with its rule and message in the same call.
- Add
noValidateto the form, or the browser's own validation runs first. - Use
valueAsNumberfor number inputs, or the value arrives as a string. - Test the unhappy path: a bad email and a quantity of zero should show messages, not silence.
Where this leaves you
One form, five fields, and a shop order now gets checked before it is accepted: Ayesha Khan, 3 kurtas, Rs 7,350, cash on delivery to Karachi. The three failures were the useful part, the guessed message text, the leaked test, and the browser validation that ran first. Take a form of your own, write the test for the bad input before the good one, and let the errors teach you what the form actually does. That is the point: it is cheaper to meet these failures here than in a customer's order.
Further reading
- React Hook Form documentation
- useForm: register
- React Hook Form: Get Started
- Zod: Stop Bad Data at the Door of Your Checkout
- Vitest Explained: Tests That Finish Before You Blink
- Vite Explained: Why Development Got Instant
- React Router: Pages That Change Without Reloading
- Zustand: React State Without the Boilerplate
Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.
Originally published at https://practicaldevblog.blogspot.com/2026/12/react-hook-form-checkout.html
