Repository navigation
Replies: 1 comment
|
For the current v1 API, use // item-form-options.ts
import { formOptions } from '@tanstack/react-form';
import { itemInsertSchema, type ItemInsertFormValues } from '@/schemas';
export const itemFormOptions = formOptions({
defaultValues: { name: '', token: '' } as ItemInsertFormValues,
validators: { onChange: itemInsertSchema },
});// item-fields.tsx
'use client';
import { withForm } from '@/components/my-components';
import { itemFormOptions } from './item-form-options';
const ItemFields = withForm({
...itemFormOptions,
render: function ItemFieldsRender({ form }) {
return (
<form.AppField name="name">
{(field) => (
<input
value={field.state.value ?? ''}
onChange={(e) => field.handleChange(e.target.value)}
onBlur={field.handleBlur}
/>
)}
</form.AppField>
);
},
});
export default ItemFields;In the parent, spread This is documented under Breaking big forms into smaller pieces. Separately, wrap the native form submit handler so the DOM event isn't passed as submit metadata: <form onSubmit={(e) => {
e.preventDefault();
e.stopPropagation();
void form.handleSubmit();
}}>
<ItemFields form={form} />
</form>The validation guide's submit example uses that wrapper. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I am trying to understand the correct way to type a
useAppForminstance when it is passed down to child components. The current documentation only shows flat examples where the form is created and used within the same component, but this does not reflect real-world usage where forms are split across multiple components.When I pass the form instance like this:
TypeScript loses all field information:
– field names do not autocomplete
–
field.state.valuebecomes{}– value-related components report type errors
What is the recommended way to correctly type the
formobject when it is passed as a prop so that all field names and value types are preserved?Clear guidance in the documentation would be very helpful, as most real applications do not use a single flat component structure.
Minimal example:
All reactions