Provides the necessary context for a form field that represents a single element in an array.
The ElementField component is used to treat each element of an array as a separate form field. It's useful when you have a dynamic list of items that you want to treat as separate fields in your form.
ElementFields should be used within the context of a Field or Fieldset component. ElementFields create their own context to scope the errors and other states of the field.
Usage
Here's an example of how you might use the ElementField component to create a dynamic list of URLs in a form.
We're able to display errors for each element of the array, as well as array-level errors for the entire fieldset.
Check out the Dynamic Fields recipe for more advanced usage of the ElementField component.
Props
The ElementField component doesn't render an element, it strictly provides context for its children. It accepts the following props:
Slot Props
The following slot props are provided for convenience and ease of composition when using the ElementField component.
Composition
Since the ElementField component doesn't render any HTML elements, it's common practice to create a wrapper component around it to have consistent styling and behavior across your forms.
For example, you may always want to render the FieldErrors component for every field. Rather than manually including it each time, you can create a wrapper <CustomElementField /> component that includes it automatically.
To maintain the type safety of the component, we'll need to use some generics, which eslint sometimes complains about, so if you see a warning, it's likely a false positive and you can ignore it.