Referencing
Reference types
There are two kinds of referencing - component and element references.
Component reference returns you the React.Component
instance (i.e. <Form>
or <CustomField>
). Element reference returns you an HTMLElement
instance rendered by the component (i.e. <form>
, <input>
or <select>
). Both are useful for different scenarios and can be accessed as described below.
Form
Component reference
Inner reference
Reference the actual <form>
element by providing the innerRef
prop to the Form
component:
You can also access the inner reference by referencing the
Form
component and taking itsinnerRef
property:this.formRef.innerRef
. This is the same as providingForm.props.innerRef
directly.
Field
Component reference
Reference the field component by providing the ref
prop.
Inner reference
To reference the actual form element behind the field use innerRef
prop.
Beware that
innerRef
will always reference the component whereField.props.fieldProps
get destructed. You must always propagate the essential field props to the actual form element for both proper functioning and referencing.
innerRef
will not work if the form element is returned by another React Component (for example, when usingstyled-components
). See the Nested element node reference example to handle those scenarios.
Nested element node
When using third-party libraries which wrap the plain form components in their own components you need to map innerRef
explicitly to return the reference to the HTMLElement
.
Do so by accessing an innerRef
prop inside your custom field component declaration:
Do not be confused, as
StyledInput.props.innerRef
is the prop expected bystyled-components,
whileInput.props.innerRef
is the prop (a function) passed to the custom field component from thecreateField()
wrapper. Different third-party solutions may expose different interface to accept the inner reference function.
Last updated