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.

`clear()` method doesn't clear the input when called in `onChange` on an uncontrolled Typeahead

See original GitHub issue

My Typeahead component:

<Typeahead
              ref="SubjectTypeahead"
              placeholder="Search Subjects"
              onChange={this.onSearchSubjects}
              options={subjectNames}/>

My onChange function:

  onSearchSubjects = (values) => {
    if (values.length > 0) {
      this.refs.SubjectTypeahead.getInstance().clear()
      this.props.onSearchSubjects(values[0])
    }
  }

When selecting a value form the typeahead, my onChange method gets called, but the value selected doesn’t get cleared.

Any idea what I might be doing wrong?

Thanks

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
agarhycommented, Dec 26, 2020

This is how it works with me in a functional component hope it help.

import React, { useState, useEffect, useRef } from 'react'
import { Typeahead, withAsync } from 'react-bootstrap-typeahead';
const AsyncTypeahead = withAsync(Typeahead);

function SearchAdmins(props) {

const typeahead = useRef(null);

const onChange = values => {
        if (values.length)
            typeahead.current.clear()        
}

return (
<AsyncTypeahead
    id="searchUsers"
    ref={typeahead}
    onChange={onChange}
    useCache={true}
/>
)

}
1reaction
jeffmcaffercommented, Jan 29, 2018

Awesome. Not sure this is a controlled/uncontrolled question though. My really code gets the list of options in as a prop. The only reason to use ref here is to get access to the component to call clear. Otherwise the DOM is not used/accessed.

In my case the Typeahead is a fast way to populate a secondary list. The list prop coming into this component does not change, the user just keeps picking more entries. So no props change, no update.

@ericgio one thing to consider is building in the notion of clearing on change. For example, a new prop clearOnChange or some such. That would address at least some of the scenarios and remove the need for ref and infinite loop checking… Of course, there are more scenarios so it is not the full answer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How would I clear a controlled input? - Stack Overflow
I have a controlled input (I'm using Material-UI TextField and Autocomplete). I'm happy for the component to control its own value, except I ......
Read more >
Uncontrolled Components - React
For example, this code accepts a single name in an uncontrolled component: class NameForm extends React.Component { constructor(props) { super(props); this.
Read more >
React interactivity: Events and state - Learn web development
As a matter of good practice, you should clear the input after your form submits, so we'll call setName() again with an empty...
Read more >
How to Clear Input Values of Dynamic Form in React
Even if you're able to create a submit button and update your app's state the way you want, clearing the forms can be...
Read more >
A component is changing an uncontrolled input to be controlled
To fix the warning, initialize the input value to an empty string, ... return ( <div> <input type="text" id="message" name="message" onChange={handleChange} ...
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