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.

focusInputOnSuggestionClick throwing error when creating input ref

See original GitHub issue

Came across an error with focusInputOnSuggestionClick when trying to automatically focus the input field.

https://codepen.io/marclemagne/pen/RjRdvr

  1. Create an input using renderInputComponent
  2. Create a ref to the input on the class (e.g., autosuggestInput)
  3. Type c, and wait for suggestions to appear
  4. Observe in the console the following error:
autosuggest.js:698 Uncaught TypeError: Cannot read property 'focus' of undefined
    at Object.onSuggestionClick [as onClick] (autosuggest.js:698)
    at Item._this.onClick (autosuggest.js:2941)
    at Object.ReactErrorUtils.invokeGuardedCallback (react-dom.js:9017)
    at executeDispatch (react-dom.js:3006)
    at Object.executeDispatchesInOrder (react-dom.js:3029)
    at executeDispatchesAndRelease (react-dom.js:2431)
    at executeDispatchesAndReleaseTopLevel (react-dom.js:2442)
    at Array.forEach (<anonymous>)
    at forEachAccumulated (react-dom.js:15423)
    at Object.processEventQueue (react-dom.js:2645)

Questions I have:

  1. Is there a problem with how we’re trying autofocus the input field?
  2. Is there a more canonical way to set focus when the component loads?

Thanks!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
marclemagnecommented, Dec 16, 2017

Thanks, @moroshko! Sorry I’m just returning to this now.

Some follow-up with my experience and how (for better or worse) I was able to get things working.

Initially, I was working with an input styled-component and my goal was to have it focus automatically.

public input: HTMLInputElement;

public componentDidMount() {
  this.input.focus();
}

public storeInputReference = (autosuggest) => {
  if (autosuggest != null) {
    this.input = autosuggest.input;
  }
}

public renderInputComponent = (inputProps) => {
  return (
    <TextInput {...inputProps} innerRef={inputProps.ref} ref={null} />
  );
}

And in the Autosuggest component add:

renderInputComponent={this.renderInputComponent}
ref={this.storeInputReference}

Note the innerRef in the renderInputComponent method. This is what allows you to access the input element within a styled-component.

I am (still) not actually 100% clear on what the ref={null} is for, but everything was working as I needed. I saw it in another issue thread.


Eventually I extended our TextInput component to focus or select itself which added a complication with my usage of AutoSuggest.

I upgraded the styled-component to be a React class component and had it create its own local ref in order to focus/select.

What I was doing above no longer worked. The TextInput would focus, as expected, and AutoSuggest would function nicely after removing the focus-related code in the AutoSuggest parent class—but I was back to getting the JavaScript TypeError.

So here’s what I am doing now:

I removed ref={this.storeInputReference} from the Autosuggest component. I removed the componentDidMount lifecycle method, as well.

public input: HTMLInputElement;

public renderInputComponent = (inputProps) => {
  return (
    <TextInput
      focused
      {...inputProps}
      getRef={(input) => this.input = input}
      ref={() => inputProps.ref(this.input)}
    />
  );
}

In our TextInput component I added a new prop called getRef which returns its local version of this.input in its componentDidMount lifecycle method. In TextInput this.input ref is set in the standard way (ref={input => this.input = input}).

Back to where I’m using AutoSuggest, I noticed that inputProps.ref was returning a function no matter if ref={...} was a prop on Autosuggest or not. So I am using the getRef prop from my TextInput component to get the TextInput input ref and setting that reference locally where I’m using AutoSuggest. Then in the ref prop being passed into TextInput, I am calling that inputProps.ref function to (ostensibly) let Autosuggest know about the reference to the input.

No more error and everything is working correctly.

It’s not exactly elegant but it seems to be working. I am sure there has got to be a cleaner, simpler way to achieve this. At the end of the day, though, it seems that AutoSuggest needs a reference to the input or it’s going to throw an error.

I am wondering if AutoSuggest should do an existence check on this.input before trying to focus using it.

I hope this proves to be somewhat helpful for anybody else having similar issues.

2reactions
kylorhallcommented, Apr 19, 2018

@marclemagne Thanks so much for that last comment. There’s like a dozen issues around refs, but this one finally solved it for me. Debugging a custom input that is a styled-component, not necessarily needing the ref as you do, ref={null} was the key.

Read more comments on GitHub >

github_iconTop Results From Across the Web

focus react input using ref throwing error when using style ...
as per this answer: React Native _this2.refs.myinput.focus is not a function. I added a focus method focus() { this.refs.
Read more >
Refs and the DOM - React
When the ref attribute is used on an HTML element, the ref created in the constructor with React.createRef() receives the underlying DOM element...
Read more >
Code coverage report for node-npmtest-react-autosuggest ...
"function" && superClass !== null) { throw new TypeError("Super expression must ... subClass.prototype = Object.create(superClass && superClass.prototype, ...
Read more >
A complete guide to React refs - LogRocket Blog
Instead, we use useRef(null) to create refs in functional components. ... The first thing we need to do is get a reference for...
Read more >
Controller | React Hook Form - Simple React forms validation
React Hook Form embraces uncontrolled components and native inputs, however it's ... value, name, ref }, fieldState: { invalid, isTouched, isDirty, error } ......
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