'input.focus is not a function' when using a custom component as the input.

See original GitHub issue

codepen http://codepen.io/dancoates/pen/JErmeO?editors=0010

steps to replicate

  • enter something in autosuggest box
  • select item from the list by clicking on it
  • there will be an error in the console along the lines of ‘input.focus is not a function’

This seems to happen when using a custom renderInputComponent function that returns a custom input component. It doesn’t seem to actually break the autosuggest but doesn’t focus the input and may cause subtle issues that I haven’t noticed yet.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
danieluhlcommented, Jun 21, 2017

Above is correct, I wanted to give an example using a custom component:

  const renderInputComponent = inputProps => {
    return <TextInput {...inputProps} refCallback={inputProps.ref} ref={null} />;
  };
  ...
  render() {
    return <Autosuggest renderInputComponent={props.renderedInputComponent} {...props} />
  }
  ...
  TextInput = props => <div><input ref={props.refCallback} /></div>;

The trick here is ref={null} otherwise the autosuggest input gets set twice, first to the input then to the TextInput component - and focus blows up.

3reactions
nathanbirrellcommented, Sep 12, 2018

A note for styled-components users who stumble across this. You can’t spread the inputProps, since the inputProps contains a ref attribute that, in styled-components, needs to be innerRef. Do something along these lines:

innerRef={inputProps.ref}

Read more comments on GitHub >

github_iconTop Results From Across the Web

ref.current.focus() is not a function - Stack Overflow
I'm using react v.16.9.0 My code is this: const handleKeyDown = (e, element, index) => { element ...
Read more >
Focus management with Vue refs - Learn web development
Inside your mounted() method, assign your labelInput ref to a variable, and then call the focus() function of the ref. You don't have...
Read more >
Refs and the DOM - React
Refs provide a way to access DOM nodes or React elements created in the render method. In the typical React dataflow, props are...
Read more >
Focusing: focus/blur - The Modern JavaScript Tutorial
The focus event is called on focusing, and blur – when the element loses the focus. Let's use them for validation of an...
Read more >
How to autofocus using React Hooks - LogRocket Blog
When the form component renders, the reference for the email input will be set. This executes the function we have in the useCallback()...
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