Build react-hook-form style useForm: uncontrolled fields, validation modes, subscribed renders and useFieldArray
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
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
- 2
- code listings
- 6
- edge cases
- 4
- follow-up questions
What's inside
- The full problem, with examples
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Stuck? Take a hint
- 1If the inputs are uncontrolled, the values do not need to live in React state at all. Where do they live, how does an input get its default value, and what is left that can make the form re-render?
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.