JedWatson / react-input-autosize

Auto-resizing input field for React
https://jedwatson.github.io/react-input-autosize
MIT License
769 stars 178 forks source link

Passing ref to AutoSizeInput Component #188

Closed vaibhav135 closed 2 years ago

vaibhav135 commented 2 years ago

Hi, I am using this package with type-definitions from here and just wanted to how I can pass ref. I have made a forward ref and passing that ref to this. I am currently passing the ref to inputRef. But it is giving me this error :-

Overload 1 of 2, '(props: AutosizeInputProps |
Readonly<AutosizeInputProps>): AutosizeInput', gave the following error.
Type 'ForwardedRef<HTMLInputElement>' is not assignable to type
'((instance: HTMLInputElement | null) => void) | undefined'.
 Type 'null' is not assignable to type '((instance: HTMLInputElement | null) => void) | undefined'.

 Overload 2 of 2, '(props: AutosizeInputProps, context: any):
AutosizeInput', gave the following error.
 Type 'ForwardedRef<HTMLInputElement>' is not assignable to type 
'((instance: HTMLInputElement | null) => void) | undefined'.
pjchender commented 2 years ago

Cause the inputRef receives a callback ref as its' props, you can not pass ref directly into it. Maybe you need to create a ref first, then pass a callback into the inputRef. For example,

const inputRef = useRef<HTMLInputElement | null>(null);
const setInputRef = useCallback((inputElement: HTMLInputElement | null) => {
  inputRef.current = inputElement;
  }, []);

<AutosizeInput inputRef={setInputRef} />
vaibhav135 commented 2 years ago

@pjchender thanks it worked!