name="firstName"
children={(field) => <field.TextField label="First Name" />}
/>``` Is it possible to call <TextField field={field} /> directly instead of using form.AppField with a children function like (field) => <field.TextField />? Can I pass the field value inside the children in another way?
#form.AppField -children-typeerror
2 messages · Page 1 of 1 (latest)
You have four options
-
useFieldContext
This is the intended way to access field values inside field components. -
Context
Since it internally uses a context, you don't need to prefix it with field. This is valid React code:
import { TextField } from "your-components";
<form.AppField name="firstName" children={() => <TextField label="First Name"/>} />
- Pass as prop
Less useful for form composition, but you can still pass values through props like so:
<form.AppField
name="firstName"
children={
(field) => <TextField label="First Name" value={field.state.value}/>
}
/>
- Typing
If you want to restrain the typing (from string to 'a' | 'b'), you can add it through the following structure
Do not use this value inside the actual field component. This is for typing only. Use Option 1 instead.
interface TextFieldProps<TFieldType extends string> {
field: {
state: {
value: TFieldType
}
}
}