ref doesn't accept a function as ref?
See original GitHub issueAutosize is not working when I pass a function instead of a ref object.
However, React does support functions. And react-textarea-autosize documentation states it does too:
<TextareaAutosize ref={(tag) => (this.textarea = tag)} />
Looking at the code, there’s the assumption that it’s an object with the current
prop. So, no check for functions
componentDidMount() {
...
if (typeof maxRows === "number" || async) {
...
setTimeout(
() => this.textarea.current && autosize(this.textarea.current)
);
} else {
this.textarea.current && autosize(this.textarea.current);
}
if (this.textarea.current) {
this.textarea.current.addEventListener(RESIZED, this.onResize);
}
}
Also, the interface is:
interface RefObject<T> {
readonly current: T | null;
}
Any clue how to make it work with a function? Am I missing something?
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Forwarding Refs - React
Regular function or class components don't receive the ref argument, and ref is not available in props either. Ref forwarding is not limited...
Read more >How to correct a #REF! error - Microsoft Support
The #REF! error shows when a formula refers to a cell that's not valid. This happens most often when cells that were referenced...
Read more >Why you can't pass a ref to a functional component in react?
There are two aspects: Function components don't have ref attribute because they don't have instances like class components.
Read more >How to use React's forwardRef function - Felix Gerschau
Our task is to forward the ref that the Input component receives to the HTML input element. We do that by wrapping the...
Read more >The argument function of React.forwardRef ... - Rule | DeepScan
This rule applies when the argument function of React.forwardRef() does not use its second ref parameter. The purpose of applying React.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This codepen is using
react-autosize-textarea
and this repository containsreact-textarea-autosize
😅It is called, I think React guarantees that. But try the textarea. It won’t grow. Then try with 7.1.0 version and it will work. The functionality is the key here. Passing a function, it breaks.
Also, if you compare the files, 7.0.0 was like this, using
this.textarea.current
:and 7.1.0 is like this:
Meaning, in the the first chunk of code
this.textarea.current
used to evaluate toundefined
, because.current
property of a function isundefined
.