Calling Mandatory Getter props
Hilly Landscape in the Village
of Şimon.
...there's a lot going on with the attributes and the handlers and the ref and the other properties...
Let's assume that we have the following scenario. We have a custom hook that returns getter props, and we want to make sure that those getter props are called correctly. But, first things first:
What is a getter prop?
It's a function that gets returned from the custom hook, which is supposed to be
called and its result used for some reason. Let's take the getInputProps
getter prop from downshift:
import * as React from 'react'
import {useCombobox} from 'downshift'
const items = ['red', 'green', 'blue']
const {getInputProps} = useCombobox({
items,
})
return (
<div>
<input {...getInputProps()} />
</div>
)
The getInputProps result is an object with properties such as:
aria-labelledby, aria-expanded, role, onChange, onClick etc. These
properties are going to make our input more or less acting like a combobox. The
combobox logic is kept inside the custom hook, the inputs are retrieved through
the event handler functions (onClick, onChange) and the combobox related
information is passed through the other attributes (aria-labelledby,
aria-expanded). Of course, there are many more handlers and properties, but we
are keeping it simple for now.
There's also the ref property that is going to be returned from
getInputProps, and this is needed in order to allow useCombobox to access
the input element and perform some custom .focus() actions when needed.
The Problem
Given that there's a lot going on with the attributes and the handlers and the
ref and the other properties, we want to make sure that the getInputProps
getter function is called correctly.
- First of all, we want to make sure that it is called.
- Secondly, that is called on an
<input>element, or in such a way that it has access to an<input>element. - Thirdly, in case it is not called correctly, we want to inform the user through a console error and give them enough information to fix the getter function call.
- Moreover, we also want to show the console error in the development environment, not production. -In addition, we would like to show this error only on the first render, as we believe it's enough in terms of checks and informations.
- Last, but not least, we will also provide an escape hatch to avoid getting
this error if the consumer just cannot call
getInputPropsproperly on the first render.- We are going to document this escape hatch and make sure the consumer knows
why we are adding it. Ideally, if a consumer will use the escape hatch, they
are aware how
getInputPropsworks, how it should be called, and if they, for some reason, cannot call it on first render (using a Portal for example), they will use the escape hatch to avoid the error, but they will test their implementation to make sure that it works as it would by callinggetInputPropsnormally.
- We are going to document this escape hatch and make sure the consumer knows
why we are adding it. Ideally, if a consumer will use the escape hatch, they
are aware how
