Skip to content
Source
Sign inSign up
TanStack/form

docs(react): document field state reactivity #2445

Mergeddgesteves(GitHub profile, opens in a new tab)merged 3 commits intomain fromrefs/pull/2445/headmerged by LeCarbonator(GitHub profile, opens in a new tab)

Showing 2 changed files with 21 additions and 0 deletions.

x
0 of 2 viewed, 2 left
6 additions and 0 deletionsdocs/framework/react/guides/form-composition.mdModified
View file
@@ -55,23 +55,29 @@
> Note: the `useFieldContext` must be the same one exported from your custom form context
​
```tsx
+import { useSelector } from '@tanstack/react-form'
import { useFieldContext } from './form-context.tsx'
​
export function TextField({ label }: { label: string }) {
// The `Field` infers that it should have a `value` type of `string`
const field = useFieldContext<string>()
+ const errors = useSelector(field.store, (state) => state.meta.errors)
return (
<label>
<span>{label}</span>
<input
value={field.state.value}
onChange={(e) => field.handleChange(e.target.value)}
+ onBlur={field.handleBlur}
/>
+ {errors.length > 0 && <em role="alert">{errors.join(', ')}</em>}
</label>
```
​
+To keep a field component in sync with meta such as `errors`, read it with `useSelector`. See [Reactivity](./reactivity.md#field-state) for details.
+​
You're then able to register this component with your form hook.
​
```tsx