question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ref doesn't accept a function as ref?

See original GitHub issue

Autosize 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:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
Andaristcommented, Dec 19, 2020

This codepen is using react-autosize-textarea and this repository contains react-textarea-autosize 😅

0reactions
gperez-10pinescommented, Dec 16, 2020

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:

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

and 7.1.0 is like this:

  componentDidMount() {
    ...
    if (typeof maxRows === "number") {
      this.updateLineHeight();
    }

    if (typeof maxRows === "number" || async) {
      ...
      setTimeout(
        () => this.textarea && autosize(this.textarea)
      );
    } else {
      this.textarea && autosize(this.textarea);
    }

    if (this.textarea) {
      this.textarea.addEventListener(RESIZED, this.onResize);
    }
  }

Meaning, in the the first chunk of code this.textarea.current used to evaluate to undefined, because .current property of a function is undefined.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found