useField()
OneForm's <Field />
comes with a lot of props to fit all components' needs, but there's no way it'll be suitable for every existing project.
That's where useField
comes into play.
This hook is meant for writing custom <Field />
components.
A better option might be useFieldData
which is designed for generic unopinionated use cases.
useField
is used by OneForm's Field
component internally which allows you to build components using the exact same tooling in your own projects.
Props
Props in
Prop Name | Description |
| The base field name without a |
| Called when the returned |
| Called when the returned |
Props out
Prop Name | Description |
| Array of error message strings. When changed, this value is updated. |
| The OneForm field name with a |
| A callback function for when a field is visited. This expects to receive an HTML |
| String value of the |
| Current field value. When changed, value is updated. |
| A callback function for when a field value changes.
This expects to receive an HTML |
When to use?
A common use case is creating your own <Field />
component.
Why though?
OneForm has opinions on how it names props passed to child components. If you want more control over this, then you'll need to create your own <Field />
component.
Here's an example of a slimmed down custom <Field />
component:
The actual <Field />
component in OneForm is much more complex, but that's because it has more generic requirements. In general, you want to tier a custom <Field />
component to your project's needs based on the component structure and props naming you want to do.
Last updated