`clear()` method doesn't clear the input when called in `onChange` on an uncontrolled Typeahead
See original GitHub issueMy 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:
- Created 6 years ago
- Reactions:1
- Comments:14 (6 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
This is how it works with me in a functional component hope it help.
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 callclear
. 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.