Uncaught TypeError: node.getBoundingClientRect is not a function
Hello,
I try to render a custom Input so my stylings will be applied, my Input-component is a class component that extends React.PureComponent but it seems the "ref"-property is not working in my current environment.
It renders but on clicking in the Input i get a "Uncaught TypeError: node.getBoundingClientRect is not a function"-Error.
I really hope that you can help me with that :)
Environment:
- React and React DOM 16.3.2
- react-autocomplete 1.8.1
Demo
Demo containing the Error: jsbin
I've found the solution: jsbin
So basically you need to pass the ref to a wrapper that forwards the ref to your class component, like this:
<ReactAutocomplete
// other ReactAutocomplete settings
renderInput ({ ref, ...props }) {
return (
<WrappedInput
ref={input => { ref(input) }}
{...props}
/>
)
}
/>
The wrapper needs to forward the ref to the "Input"-class component, like this:
const WrappedInput = React.forwardRef((props, ref) => (
<Input forwardedRef={ref} {...props} />
))
and the wrapped Input must set the ref, like this:
class Input extends React.PureComponent {
render () {
const { forwardedRef, ...props } = this.props
return (
<input ref={forwardedRef} {...props} />
)
}
}
It would of course be awesome if that would be documented, which i would like to do if there are no concerns
@dnepro This doesn't seem to be working for me, I'm using a functional component (eg. TextInput) with a styled component as the input. I've tried what you've done but I'm getting TextInput: ref is not a prop.
Yes, you're right but because you can't use a functional component, it must be a stateful component (e.g. class TextInput ...)
@dnepro Got it!
On the React AutoComplete Component
renderInput={props => {
const { ref, ...rest } = props;
return (
<TextInput
reference={ref}
{...rest}
error={errors.school_name}
inputId='school_name'
inputValue={school_name}
labelText='Name of Institution'
name='school_name'
placeholder='Enter Name of Institution...'
required
/>
);
}}
Then in my TextInput.js I changed it to a stateful component as you said
I had to utilize innerRef={this.props.reference} from the styled-components documentation.
Ahh yes, i also had the same but i didn't put it 🤣