Skip to content

Commit 1c65b30

Browse files
harry-whorlowHarry Whorlow
and
Harry Whorlow
authored
docs(react): emphasize form composition exports (#1231)
* docs(react): emphasise form composition exports * chore: slight tweek --------- Co-authored-by: Harry Whorlow <[email protected]>
1 parent f953ec8 commit 1c65b30

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

docs/framework/react/guides/form-composition.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: form-composition
33
title: Form Composition
44
---
55

6-
A common criticism of TanStack Form is its verbosity out-of-the-box. While this _can_ be useful for educational purposes - helping enforce understanding our APIs - it's not ideal in production usecases.
6+
A common criticism of TanStack Form is its verbosity out-of-the-box. While this _can_ be useful for educational purposes - helping enforce understanding our APIs - it's not ideal in production use cases.
77

88
As a result, while `form.Field` enables the most powerful and flexible usage of TanStack Form, we provide APIs that wrap it and make your application code less verbose.
99

@@ -18,7 +18,9 @@ At it's most basic, `createFormHook` is a function that takes a `fieldContext` a
1818
```tsx
1919
import { createFormHookContexts, createFormHook } from '@tanstack/react-form'
2020

21-
const { fieldContext, formContext } = createFormHookContexts()
21+
// export useFieldContext for use in your custom components
22+
export const { fieldContext, formContext, useFieldContext } =
23+
createFormHookContexts()
2224

2325
const { useAppForm } = createFormHook({
2426
fieldContext,
@@ -45,9 +47,12 @@ function App() {
4547

4648
Once this scaffolding is in place, you can start adding custom field and form components to your form hook.
4749

50+
> Note: the `useFieldContext` must be the same one exported from your custom form context
51+
4852
```tsx
49-
function TextField({ label }: { label: string }) {
50-
// Use the context returned from `createFormHookContexts`
53+
import { useFieldContext } from './form-context.tsx'
54+
55+
export function TextField({ label }: { label: string }) {
5156
// The `Field` infers that it should have a `value` type of `string`
5257
const field = useFieldContext<string>()
5358
return (
@@ -65,6 +70,8 @@ function TextField({ label }: { label: string }) {
6570
You're then able to register this component with your form hook.
6671

6772
```tsx
73+
import { TextField } from './text-field.tsx'
74+
6875
const { useAppForm } = createFormHook({
6976
fieldContext,
7077
formContext,

0 commit comments

Comments
 (0)