Capítulo 7 de 29
Four pieces make up every form: the form instance (useForm), a field instance (form.Field), field state (metadata + value + validation status), and an explicit subscription model that opts components into re-rendering rather than doing it automatically.
useForm returns — the control hub for the whole form's lifecycle, field registration, and submission.form.Field wraps one input, tracking its own value/validation independently of siblings.field.state): value, validation status, error messages, plus interaction flags — isTouched, isDirty, isBlurred.useSelector(form.store, selector) (logic-level) or form.Subscribe (UI-level) — see ch014 for the full reactivity model this enables.const form = useForm({
defaultValues: { name: '' },
onSubmit: async ({ value }) => console.log(value),
})
const name = useSelector(form.store, (state) => state.values.name)
<form.Field
name="name"
validators={{ onChange: ({ value }) => value ? undefined : 'Required' }}
children={(field) => (
<input value={field.state.value} onChange={(e) => field.handleChange(e.target.value)} />
)}
/>
useSelector) alongside a validated field, showing the two concerns (reading state, validating a field) as separate mechanisms.field.state is the one place to look for a field's current value, validity, and interaction history — don't track touched/dirty separately yourself.useSelector/form.Subscribe) instead of reading the whole form is the default, not an optimization to add later (ch014).onChangeListenTo, ch013).validators prop shown in the example.