react-autocomplete icon indicating copy to clipboard operation
react-autocomplete copied to clipboard

Uncaught TypeError: node.getBoundingClientRect is not a function

Open dnepro opened this issue 8 years ago • 6 comments

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

dnepro avatar Aug 03 '18 11:08 dnepro

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} />
    )
  }
}

dnepro avatar Aug 03 '18 12:08 dnepro

It would of course be awesome if that would be documented, which i would like to do if there are no concerns

dnepro avatar Aug 03 '18 12:08 dnepro

@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.

dongarchive avatar Aug 03 '18 15:08 dongarchive

Yes, you're right but because you can't use a functional component, it must be a stateful component (e.g. class TextInput ...)

dnepro avatar Aug 03 '18 15:08 dnepro

@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.

dongarchive avatar Aug 03 '18 16:08 dongarchive

Ahh yes, i also had the same but i didn't put it 🤣

dnepro avatar Aug 03 '18 16:08 dnepro