focusInputOnSuggestionClick throwing error when creating input ref
See original GitHub issueCame across an error with focusInputOnSuggestionClick
when trying to automatically focus the input field.
https://codepen.io/marclemagne/pen/RjRdvr
- Create an input using
renderInputComponent
- Create a
ref
to the input on the class (e.g.,autosuggestInput
) - Type
c
, and wait for suggestions to appear - 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:
- Is there a problem with how we’re trying autofocus the input field?
- Is there a more canonical way to set focus when the component loads?
Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:5 (1 by maintainers)
Top 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 >
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 Free
Top 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
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.And in the
Autosuggest
component add:Note the
innerRef
in therenderInputComponent
method. This is what allows you to access theinput
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 theAutosuggest
component. I removed thecomponentDidMount
lifecycle method, as well.In our
TextInput
component I added a new prop calledgetRef
which returns its local version ofthis.input
in itscomponentDidMount
lifecycle method. InTextInput
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 ifref={...}
was a prop onAutosuggest
or not. So I am using thegetRef
prop from myTextInput
component to get theTextInput
input
ref and setting that reference locally where I’m using AutoSuggest. Then in theref
prop being passed intoTextInput
, I am calling thatinputProps.ref
function to (ostensibly) letAutosuggest
know about the reference to theinput
.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.
@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.