Build react-hook-form style useForm: uncontrolled fields, validation modes, subscribed renders and useFieldArray

hardAbout 70 minutesUpdated CodingProreactformsreact-hook-formvalidationfield-arraysperformance
Not solved

Share this question

Problem

react-hook-form became the default form library because a 200-field form can take keystrokes without re-rendering anything. It does that by leaving inputs uncontrolled (the DOM keeps the value), keeping values in a mutable object, and re-rendering only for the parts of form state a component actually reads. Senior interviews ask you to explain or rebuild that. Build the core.

const { register, control, handleSubmit, formState } = useForm({ defaultValues: { items: [{ title: "" }] } });
const { fields, append, remove } = useFieldArray({ control, name: "items" });

{fields.map((field, index) => (
  <input key={field.id} {...register(`items.${index}.title`, { required: true })} />
))}

Ref. UR-1435 · © UIRound · uiround.com/questions/build-react-hook-form-style-useform-uncontrolled-fields-validation-modes-subscribed-renders-and-usefieldarray

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment