---
title: 'Hooks'
description: 'Hooks for form state and view recipes.'
order: 4
---

# Hooks

The hooks of `@schema-engine/forms-react/hooks`: the view hooks for recipe blocks, the form hooks for widgets and the engine hooks for the form's wizard.

## View hooks

Recipe shells and blocks can access the active view with:

```tsx
import { useViewContext, useFormNavigation, useActiveStep, useViewConfig } from '@schema-engine/forms-react/hooks';
import { MinimalProgressConfigSchema } from '@schema-engine/forms/recipes/built-in';

function ProgressBlock() {
	const { steps, currentStepIndex } = useViewContext();
	const navigation = useFormNavigation();
	const activeStep = useActiveStep();
	const { showPercentage } = useViewConfig('progress', MinimalProgressConfigSchema);

	return (
		<div>
			{activeStep.title}: {currentStepIndex + 1} / {steps.length}
		</div>
	);
}
```

`useViewConfig(slot, schema)` reads the configuration for the current recipe slot
and returns the slot schema's output: `validateForm` already filled every
default, so blocks need no fallbacks. It is available only inside a `ViewProvider` or a slot block rendered by a recipe.

## Form hooks

Import the form hooks from `@schema-engine/forms-react/hooks`.

`useFormContext` exposes `getValues`, `setValue`, `validate`, `reset`, `setFocus`,
`formatIssue` and `clearSubmission`. `validate()` validates the whole form, shows its errors and resolves
`{ outcome: 'passed' | 'invalid' | 'overtaken' }`; `'overtaken'` is no verdict, so ask again. It also has a lazy `formState` (`errors`, `isValid`
and `submission: { status, failures, submitCount }`, the runtime's: `submitCount` counts the submissions
that passed or failed validation). Its fields
subscribe the caller only when read, so a component that reads `submission`
re-renders when that changes and not while the user types; reading `isValid`
reports the recorded errors and validates nothing. `useWatch` (no argument, a path
or a list of paths) and `useFieldArray` cover observation and array editing.
`useFieldArray` provides `fields` plus `append`, `prepend`, `insert`, `swap`, `move`,
`update`, `replace`, and `remove`.

### useFormField

`useFormField` binds a widget to one field. [Writing a widget](/docs/forms-react/widgets) has the full uncontrolled and controlled examples, what the form reads from a native control and how it reports the browser's validity. It has three forms:

| Call                                         | Returns                                                                                                                                                                                                                                 |
| -------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `useFormField(config)`                       | An uncontrolled field: the state (`error`, `warning`, `issue`, `invalid`, `isTouched`, `isDirty`, `isValidating`), `required`, `disabled`, `defaultValue`, and the props `fieldProps`, `labelProps`, `errorProps` and `containerStyles` |
| `useFormField(config, { controlled: true })` | The same, plus `value`, `onChange(value)` and `onBlur()`. `fieldProps` has no change handler                                                                                                                                            |
| `useFormField(path)`                         | The state and texts of a path alone (`FORM_ROOT` is the form), with no value. `useWatch(path)` reads the value                                                                                                                          |

`fieldProps` carries `ref`, `id`, `name`, `required`, `disabled`, `aria-*`, `onChange` and `onBlur`. A widget receives its configuration as props, so it takes the config itself: `function TextField(config: TextFieldConfig)`.

## Engine hooks

Import engine hooks from `@schema-engine/forms-react/hooks`.

`useFormEngine` reads the `FormEngine` supplied by `FormEngineProvider`, while
`useFormWizard` exposes the form's wizard: `current` (the id of the current step), `steps` (every step in
order, each with its `id`, `title`, `skipped`, `visited`, `complete`, `invalid` and `reachable`), `navigating`, and the commands `next()`, `back()`
and `goTo(id)`. Each command resolves to one outcome string: `'moved'`, `'invalid'` (the step has errors, which are
shown and focused), `'failed'` (a rule could not run, so the step has no verdict and the cause is logged), `'overtaken'` (an edit outran the check), `'refused'` (nowhere to go, or a skipped or
unreachable step), `'busy'` (another command is running) or `'cancelled'` (a reset or an unmount revoked it).
Submitting before the last step of a wizard is a Next. Steps are keyed by id, and a step that the answers skip
is passed over by `next` and `back` and refused by `goTo`.

`useFormRuntime` returns the form's whole runtime, for what takes a form over, such as the [agent tool](/docs/forms/agent-tool).

The form owns all of this and the submission, so `useFormContext().reset`, an action's `form.reset` and a native
reset button all return the form to its first step with an idle submission. See [Wizard](/docs/forms/wizard) for
a stepper built on `useFormWizard`.
